How to Create Migration in Laravel

How to Create Migration in Laravel

    A lot of junior developer or not trained well developers create database manually. But this is not professional. Here we will take a professional look how to create laravel migrations using command line 

    php artisan make:migration create_tablename_table

    The above command would create a migration file using corresponding table name.  It's better to have a plural form of your table name which is a good convention.

    Creating migration files

    Say you wanna create a table name courses for your application. Then in your migration command you should use courses which is a plural form of course. So I would do the below command

    php artisan make:migration create_courses_table

    Then we will see in our database/migrations folder there is a new file. The file may look like this 

    <?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('courses', function (Blueprint $table) {
                $table->id();
                $table->timestamps();
            });
        }
    
        /**
         * Reverse the migrations.
         */
        public function down(): void
        {
            Schema::dropIfExists('courses');
        }
    };
    

    At this moment if you run

    php artisan migrate

    It will create a table name courses with three fields

    1. id
    2. created_at
    3. updated_at

    But of course, you don't want to create simple table like that, it's no use. So it's time to add some fields in our migration tables. 

    We will add the below fields for our database

    1. name
    2. user_token
    3. thumbnail
    4. description
    5. video
    6. type_id

    So now, in our file up() method look like this 

     public function up(): void
        {
            Schema::create('courses', function (Blueprint $table) {
                $table->id();
                $table->string('user_token', 50);
                $table->string('name', 200);
                $table->string('thumbnail', 200)->nullable();
                $table->string('video', 200)->nullable();
                $table->text('description')->nullable();
                $table->smallInteger('type_id');
                $table->timestamps();
            });
        }

    Now, if you run 

    php artisan migrate

    You will see the below 

    Now, if your check your database you will find that, there's a table name courses there.

    Changing migration files

    If you want to make changes, to your database then first you should do

    php artisan migrate:rollback

    It will drop the database table. But your migration file is still in the folder of database/migratoins

    Then make the necessary changes in your migration files. After that you should run 

    php artisan migrate

     

    Courses


    Recommended posts


    Recent posts