Command
src/Models directory.
Parameters
The name of the model classExamples:
PostUserAdmin/Product
Options
The name of the module where the model should be createdExample:
--module=blogCreate a new migration file for the model
Create a new factory for the model
Create a new seeder for the model
Create a new controller for the model
Create a resource controller for the model (implies
-c)Create an API controller for the model
Create a new policy for the model
Generate migration, seeder, factory, policy, resource controller, and form requests
Indicates if the generated model should be a custom intermediate table model
Indicates if the generated model should be a custom polymorphic intermediate table model
Examples
Basic Model
app-modules/blog/src/Models/Post.php
Model with Migration
- Model:
app-modules/blog/src/Models/Post.php - Migration:
app-modules/blog/database/migrations/YYYY_MM_DD_HHMMSS_create_posts_table.php
Migration file
Migrations are created in the module’s
database/migrations directory and will be automatically discovered by Laravel.Model with Factory
- Model:
app-modules/blog/src/Models/Post.php - Factory:
app-modules/blog/database/factories/PostFactory.php
Factory file
Complete Model Setup
- Model:
Post.php - Migration:
create_posts_table.php - Factory:
PostFactory.php - Resource Controller:
PostController.php
All Associated Files
- Model
- Migration
- Factory
- Seeder
- Policy
- Resource Controller
- Form Requests (Store and Update)
Nested Model
app-modules/ecommerce/src/Models/Admin/Product.php
Namespace: Modules\Ecommerce\Models\Admin
Module-Specific Behavior
Default Namespace
When using the--module option, models are created in the Models directory within your module’s namespace:
getDefaultNamespace() method in the source code:
MakeModel.php:12-19
Factory Namespace Resolution
When generating factories with--factory, the package automatically adjusts the factory namespace to match your module:
MakeModel.php:21-34
HasFactory trait correctly resolves your module’s factory:
Working with Models
Using Module Models
Reference models using their full namespace:Using Factories
Model Relationships
When defining relationships between module models:Best Practices
Generate related files together
Generate related files together
Use descriptive model names
Use descriptive model names
Model names should be singular and describe a single entity:
- ✅
Post,Comment,User - ❌
Posts,CommentModel,UserData
Organize complex domains
Organize complex domains
Use subdirectories for complex modules:
Keep models focused
Keep models focused
Each model should represent a single database table. Use traits, observers, and services for complex business logic.
See Also
- make:migration - Generate migration files
make:factory --module=- Generate model factories (see Make Commands Overview)- make:controller - Generate controllers
- Laravel Eloquent Documentation