In Laravel 7, you can create a model along with a migration using a single Artisan command. The command syntax is:
Explanation:
-
make:model Post
→ Creates a new model namedPost
inside theapp
directory (default in Laravel 7). -
-m
or--migration
→ Generates a migration file for thePost
model in thedatabase/migrations
directory.
Example Output:
After running the command, Laravel will generate:
-
A model file:
app/Post.php
-
A migration file:
database/migrations/202x_xx_xx_xxxxxx_create_posts_table.php
Customizing the Model and Migration:
You can also include a factory, seeder, controller, and resourceful controller in a single command:
-
-m
→ Creates a migration. -
-f
→ Creates a factory. -
-s
→ Creates a seeder. -
-c
→ Creates a controller. -
-r
→ Makes the controller resourceful (includes index, show, create, store, edit, update, destroy methods).
Example Migration File:
Inside database/migrations/202x_xx_xx_xxxxxx_create_posts_table.php
:
Run the migration with:
Would you like me to include more details, such as defining relationships or using factories?