How To Create Migration Using Artisan Command In Laravel

  Jan 2024
  ITSolutionsGuides
  Category: Laravel
How To Create Migration Using Artisan Command In Laravel

Welcome To ITSolutionsGuides,


Lets see How To Create Migration Using Artisan Command In Laravel using it solutions guides tutorial. By default while creating the new laravel project it will generate four basic default migration files. We need to create new migration tables as per our requirement. We can create many migration tables as required depends on the projects. Creating the migration table in laravel is not so complicated since laravel provides the artisan command to create new migration tables.
In this example we will see,

1) Creating only the migration table using artisan command.
2) Creating both modal and migration table using artisan command.
Explore seamless Laravel migration creation with Artisan commands. Elevate your development game by mastering this essential skill effortlessly.

Let's Start Coding

In Laravel new migration table can be created by using the following artisan command,
php artisan make:migration create_products_table
the above artisan command will create the migration file in the below location. It will create a simple migration layout and we need to add our required columns as per required,


database/migration/2014_10_12_000000_create_users_table.php

<?php

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

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

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

Let's Start Coding

In Laravel we can create both the model and the migrations using the single artisan command,
php artisan make:model Product -m
after running the artisan command it will create both the modal and the migration files. It will create the migration file same as above and also the model file below,

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;
}

Let's Start Coding

The above artisan command will just create the migration files in our project locally and will not create the migration table in the database. To create the migration table in the database we need to run another artisan command. After changing the columns in the migration files we should run the migration artisan command to create the table in the database.

php artisan migrate

We hope it helps everyone. Thanks for supporting ITSolutionsGuides and keep supporting us also follow us in social media platforms.

Subscribe for NewsLetter

Be the first to know about releases and tutorial news and solutions.

We care about your data in our privacy policy.

ITSolutionsGuides

ITSolutionsGuides was started mainly to provide good and quality web solutions for all the developers. We provide tutorials to support all the developers and also we try to provide solutions to the errors we face while coding.

Contact US

ITSolutionsGuides, provide users with an easy-to-use form to reach out for support or inquiries.

whatsapp  gmail  instagram-new--v1  facebook-circled  twitter-circled  linkedin  github  pinterest 

Copyright © 2023 - 2024 All rights reserved | ITSolutionsGuides