Mastering Laravel Model Commands
When I first started working with Laravel, I remember creating models the “hard way” - manually creating files, writing boilerplate code, and then scrambling to create the corresponding migrations, factories, and controllers. Those days feel like a distant memory now, thanks to Laravel’s incredibly powerful model commands that can set up an entire feature scaffold in seconds.
Let me walk you through everything you need to know about Laravel’s model commands, from the basics to some advanced techniques that have saved me countless hours of development time.
Basic Model Creation
The most straightforward command is the one we all learn first:
php artisan make:model User
This creates a simple model file in app/Models/User.php
. Nothing fancy, just a clean Eloquent model ready for you to customize. But here’s where most tutorials stop, and where the real magic begins.
Model with Migration
Almost every model needs a database table, so why create them separately?
php artisan make:model Post -m
The -m
flag creates both the model and its corresponding migration file. I can’t tell you how many times this simple addition has saved me from forgetting to create migrations or getting the naming conventions wrong.
Going All Out - The Full Stack Approach
Now, here’s where Laravel really shines. When building a new feature, you typically need a model, migration, controller, factory, seeder, and maybe some form requests. Instead of running six separate commands, you can do this:
php artisan make:model Product -mfsc
Let me break down what each flag does:
-m
: Creates a migration-f
: Creates a model factory-s
: Creates a seeder-c
: Creates a resource controller
This single command creates five files with proper naming conventions and relationships. It’s like having a junior developer who never makes naming mistakes and works at the speed of light.
The Controller Variations
Sometimes you need different types of controllers. Laravel’s got you covered:
# Creates a resource controller with all CRUD methods
php artisan make:model Order -mc
# Creates an API resource controller (no create/edit methods)
php artisan make:model ApiResource -mc --api
# Creates a controller with custom resource methods
php artisan make:model CustomModel -mc --resource
Advanced Flag Combinations
Over the years, I’ve developed some go-to combinations based on different project needs:
For a complete feature with testing:
php artisan make:model Feature -mfsc --pest
For API endpoints:
php artisan make:model ApiModel -mfc --api
For pivot models (many-to-many relationships):
php artisan make:model UserRole -m --pivot
The Subdirectory Organization
As your application grows, you’ll want to organize models into subdirectories. Laravel handles this beautifully:
php artisan make:model Blog/Post -mfsc
This creates the model in app/Models/Blog/Post.php
and automatically adjusts the namespace. The migration, factory, and other files are created with the correct naming conventions too.
Working with Existing Models
Sometimes you need to add components to existing models. Laravel allows you to create individual pieces:
# Add a factory to an existing model
php artisan make:factory PostFactory --model=Post
# Add a seeder for an existing model
php artisan make:seeder PostSeeder
# Add a resource controller for an existing model
php artisan make:controller PostController --resource --model=Post
The Force Flag - When You Need to Override
There are times when you need to recreate files (maybe you messed up and want to start fresh):
php artisan make:model Post -mfsc --force
Be careful with this one - it will overwrite existing files without warning.
Custom Stubs
Here’s an advanced tip that many developers don’t know about. You can customize the templates (stubs) that Laravel uses to generate models:
php artisan stub:publish
This publishes all the stub files to stubs/
in your project root. You can then modify stubs/model.stub
to include your own default imports, traits, or methods that every model should have.
Real-World Workflow
In my daily development, here’s how I typically approach new features:
- Start with the full stack:
php artisan make:model Feature -mfsc
- Configure the migration with proper columns and relationships
- Set up the factory with realistic fake data
- Define model relationships and any custom methods
- Run migrations and seed some test data
This workflow ensures I have everything I need to start building features immediately, with proper testing data and a solid foundation.
Common Pitfalls to Avoid
Naming Conventions: Laravel expects singular model names (User
, not Users
). The framework handles pluralization automatically.
Namespace Issues: When creating models in subdirectories, make sure your relationships reference the correct namespaced models.
Migration Ordering: If your models have relationships, create them in the right order or use foreign key constraints properly.
The Bottom Line
Laravel’s model commands aren’t just time-savers - they’re consistency enforcers. They ensure proper naming conventions, correct file placement, and consistent structure across your application. Once you master these commands, you’ll wonder how you ever developed without them.
The next time you’re about to create a new feature, resist the urge to create files manually. Let Laravel’s generators do the heavy lifting, and spend your time on what really matters - building great features for your users.