Laravel 7 create model,and migration in single artisan command

Laravel 7 create model,and migration in single artisan command


In Laravel 7, you can create a model along with a migration using a single Artisan command. The command syntax is:

php artisan make:model Post -m

Explanation:

  • make:model Post → Creates a new model named Post inside the app directory (default in Laravel 7).

  • -m or --migration → Generates a migration file for the Post model in the database/migrations directory.

Example Output:

After running the command, Laravel will generate:

  1. A model file: app/Post.php

  2. 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:

php artisan make:model Post -mfscr
  • -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:

public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('content'); $table->timestamps(); }); }

Run the migration with:

php artisan migrate

Would you like me to include more details, such as defining relationships or using factories?

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
close