Skip to content

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.

  1. Generate the model and its factory (-f is --factory).

    Terminal window
    php artisan module:make-model Post Blog --fillable=title,body -f

    This creates Modules/Blog/app/Models/Post.php and Modules/Blog/database/factories/PostFactory.php.

    Modules/Blog/app/Models/Post.php
    <?php
    namespace 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 under Database\Factories\Modules\..., so #[UseFactory] tells Post::factory() where the module factory lives.

    To generate Laravel 13 Eloquent attributes (#[Fillable([...])]) instead of a $fillable property, add --attributes:

    Terminal window
    php artisan module:make-model Post Blog --fillable=title,body --attributes
  2. Implement definition() in the factory.

    Modules/Blog/database/factories/PostFactory.php
    public function definition(): array
    {
    return [
    'title' => fake()->sentence(),
    'body' => fake()->paragraph(),
    ];
    }
  3. Check the result. Inspect the model and create a record with the factory.

    Terminal window
    php artisan module:model-show Post

    module:model-show resolves Post across your modules (pass 'Modules\Blog\Models\Post' if another module or the app defines Post).

    Modules\Blog\Models\Post ...................................................
    Database ............................................................ sqlite
    Table ................................................................ posts
    Terminal window
    php artisan tinker --execute="echo Modules\Blog\Models\Post::factory()->create()->title;"

    The command prints the generated post title.

Combine flags to run extra generators alongside module:make-model:

Flag Also creates
-m Migration create_posts_table
-c PostController
-s PostSeeder
-p PostPolicy
Terminal window
php artisan module:make-model Post Blog -mfcs

Without -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.

Generate an observer or policy inside the module:

Terminal window
php artisan module:make-observer Post Blog
php artisan module:make-policy PostPolicy Blog

module: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 call Post::observe(PostObserver::class) in the module service provider.
  • Policy: call Gate::policy() in your module service provider’s boot() method (always call parent::boot()):
Modules/Blog/app/Providers/BlogServiceProvider.php
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:

Modules/Blog/app/Models/Post.php
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Modules\Shop\Models\Product;
public function product(): BelongsTo
{
return $this->belongsTo(Product::class);
}
Modules/Shop/app/Models/Product.php
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.