Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

create model with migration

How to create a model with migration in Laravel?

Today, I will give you an example of “How to create a model with migration in Laravel and “How to create a model, controller, and migration in single artisan command in Laravel”. So you can easily apply it with your laravel 6, laravel 7, and Laravel 8 application.

First, what we’re doing here, This is the example :

create model with migration in laravel

Laravel - create model, controller and migration in single artisan command

Let’s get started.

Create Model with Migration

Using the given below command We create a model with migration.

php artisan make:model Role -m
create model with migration in laravel

Database\Migrations\Migration\Roles

<?php

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

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

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

app\Models\Role.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Role extends Model
{
    use SoftDeletes;
    use HasFactory;
}

app\Http\Controllers\RoleController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class RoleController extends Controller
{
    //
}

Create Model with Migration and Controller

Using the given below command We create a model and controller with migration.

php artisan make:model UserRole -c -m
Laravel - create model, controller and migration in single artisan command
  • -m, to create migration
  • -c to create controller

Database\Migrations\Migration\UserRoles

<?php

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

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

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

app\Models\UserRole.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserRole extends Model
{
    use SoftDeletes;
    use HasFactory;
}

app\Http\Controllers\UserRoleController .php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class UserRoleController extends Controller
{
    //
}

If you run php artisan make:model –help you can see all the available options

php artisan make:model YourModelName -help
php artisan -help command in laravel

You will notice one thing in all the generated migrations and models the soft delete field automatically adds.

You can use stubs in your Laravel application to reduce some time to add on every command, we can manage it only once in the migration stub and model stub.

stubs in laravel

We learned “How to create a model with migration in Laravel”, I hope this article will help you with your Laravel application Project.

Read also: List of most useful Artisan Commands in Laravel.