Add Eloquent models to a module
To create a Post model and its factory in the Blog module, generate both with one command. You need an enabled Blog module and the posts table from Create and run module migrations.
-
Generate the model and its factory (
-fis--factory).Terminal window php artisan module:make-model Post Blog --fillable=title,body -fThis creates
Modules/Blog/app/Models/Post.phpandModules/Blog/database/factories/PostFactory.php.Modules/Blog/app/Models/Post.php <?phpnamespace Modules\Blog\Models;use Illuminate\Database\Eloquent\Attributes\UseFactory;use Illuminate\Database\Eloquent\Factories\HasFactory;use Illuminate\Database\Eloquent\Model;use Modules\Blog\Database\Factories\PostFactory;#[UseFactory(PostFactory::class)]class Post extends Model{/** @use HasFactory<PostFactory> */use HasFactory;/*** The attributes that are mass assignable.*/protected $fillable = ["title","body"];}Keep the
#[UseFactory]attribute. Laravel’s default factory guesser looks underDatabase\Factories\Modules\..., so#[UseFactory]tellsPost::factory()where the module factory lives.To generate Laravel 13 Eloquent attributes (
#[Fillable([...])]) instead of a$fillableproperty, add--attributes:Terminal window php artisan module:make-model Post Blog --fillable=title,body --attributes -
Implement
definition()in the factory.Modules/Blog/database/factories/PostFactory.php public function definition(): array{return ['title' => fake()->sentence(),'body' => fake()->paragraph(),];} -
Check the result. Inspect the model and create a record with the factory.
Terminal window php artisan module:model-show Postmodule:model-showresolvesPostacross your modules (pass'Modules\Blog\Models\Post'if another module or the app definesPost).Modules\Blog\Models\Post ...................................................Database ............................................................ sqliteTable ................................................................ postsTerminal window php artisan tinker --execute="echo Modules\Blog\Models\Post::factory()->create()->title;"The command prints the generated post title.
Generate more files with the model
Section titled “Generate more files with the model”Combine flags to run extra generators alongside module:make-model:
| Flag | Also creates |
|---|---|
-m |
Migration create_posts_table |
-c |
PostController |
-s |
PostSeeder |
-p |
PostPolicy |
php artisan module:make-model Post Blog -mfcsWithout -f, the model omits HasFactory. To add a factory later, run php artisan module:make-factory Post Blog and add #[UseFactory(PostFactory::class)] and use HasFactory; to the model.
Register observers and policies
Section titled “Register observers and policies”Generate an observer or policy inside the module:
php artisan module:make-observer Post Blogphp artisan module:make-policy PostPolicy Blogmodule:make-observer appends Observer (Modules/Blog/app/Observers/PostObserver.php). module:make-policy uses the name as given (Modules/Blog/app/Policies/PostPolicy.php).
Laravel does not auto-discover observers or policies under Modules\. Register them explicitly:
- Observer: attach
#[ObservedBy([PostObserver::class])]to the model class, or callPost::observe(PostObserver::class)in the module service provider. - Policy: call
Gate::policy()in your module service provider’sboot()method (always callparent::boot()):
use Illuminate\Support\Facades\Gate;use Modules\Blog\Models\Post;use Modules\Blog\Policies\PostPolicy;
public function boot(): void{ parent::boot();
Gate::policy(Post::class, PostPolicy::class);}Relate a model to a model of another module
Section titled “Relate a model to a model of another module”Import the other module’s model class directly:
use Illuminate\Database\Eloquent\Relations\BelongsTo;use Modules\Shop\Models\Product;
public function product(): BelongsTo{ return $this->belongsTo(Product::class);}use Illuminate\Database\Eloquent\Relations\HasMany;use Modules\Blog\Models\Post;
public function posts(): HasMany{ return $this->hasMany(Post::class);}php artisan migrate runs migrations in filename (timestamp) order. Create any migration that adds a foreign key after the migration that creates the referenced table.