How to Create Model And Migration Using a Single Artisan Command

To generate a model and a migration file you need to run two separate commands.

Quick Tip : How to Create a Service Provider in Laravel 5.1

For model,

php artisan make:model Ticket

And For migration,

php artisan make:migration create_tickets_table --create=tickets

But, while going through Laravel documentation, I have found out that you can do both of these tasks in a single command.

You can use anyone of the following commands to generate both of these files at the same time.

php artisan make:model Ticket --migration

Or,

php artisan make:model Ticket -m

After executing anyone of the above commands we will get a,

Ticket.php (Model):

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Ticket extends Model
{
    //
}

And 2015_11_22_122910_create_tickets_table.php (Migration):

<?php

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

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

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

In case you have any query, feel free to comment.

Leave a Comment.