Migrations are like version control for your database in Laravel applications, allowing our team to define and share the application's database schema definition very easily and efficiently. If you have ever had to tell a teammate to manually add a column to the local database schema after pulling in your changes from source control, then we have to face the problem that Laravel database migrations solve.

In this Laravel migrations tutorial, I am going to discuss laravel 9 create model with migration. You also see laravel 9 migration add column from this example tutorial. So let's start our laravel 9 migration guide tutorial.

Laravel provides a make:migration command to generate an initial table schema for Laravel migration. The newly created migration file will be placed in your database/migrations directory. Each migration filename contains a timestamp that allows Laravel to determine the order of the migrations and it follows ascending order:

If you run this command then it will generate a flights table schema for you.

php artisan make:migration create_flights_table

 

Now you will get the files like:

database/migrations

<?php

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

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

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

 

Now if you want to laravel 9 migration add column, then you need to follow the below guidance:

<?php
 
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
 
return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('flights', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('airline');
            $table->timestamps();
        });
    }
 
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('flights');
    }
};

 

Now if you run php artisan migrate command then this schema will create id, name, airline, created_at and updated_at columns. 

Laravel 9 Create Model With Migration

Now, in this step, we will create migrations with the model. Laravel make:model command to create model and we can create migration also for that model with attaching the -m which indicate migration for that model. To create laravel 9 make model with migration, then run the below command:

php artisan make:model Product -m

 

Now this command will generate a Product model inside app/Models directory.

app/Models/Product.php

<?php

namespace App\Models;

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

class Product extends Model
{
    use HasFactory;
}

 

Read also: How To Connect SQlite Database In Laravel?

 

Conclusion

I have tried to discuss the clear concept of laravel 9 make model with migration. Now we know laravel 9 create model with migration. Hope this laravel 9 create model with migration tutorial will help you.