How to create migration file with Make:model command

Recently I’ve found out a nice little way to speed up generating of database stuff – I used to use make:migration and make:model Artisan commands separately. Apparently, they can be combined into one.

So if you run a command like this:

artisan make:model Books -m

It will create a migration file automatically for you. The magic is in -m parameter.

The whole thing looks like this:

artisan make:model Books -m
Model created successfully.
Created Migration: 2015_10_19_012129_create_books_table

And the result is that we have two files generated:

app/Books.php:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Books extends Model
{
    //
}

And database/migrations/2015_10_19_012129_create_books_table.php:

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateBooksTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('books', function (Blueprint $table) {
            $table->increments('id');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('books');
    }
}

Notice that migration file comes with automatically suggested auto_increment field and timestamps. Isn’t that sweet of Laravel? Constantly saving our time, second by second.

Like our articles?
Check out our Laravel online courses!

4 COMMENTS

LEAVE A REPLY

Please enter your comment!
Please enter your name here