php migration code example

Example 1: laravel foreign key

Schema::table('posts', function (Blueprint $table) {
    $table->unsignedBigInteger('user_id');

    $table->foreign('user_id')->references('id')->on('users');
});
OR
Schema::table('posts', function (Blueprint $table) {
    $table->foreignId('user_id')->constrained();
});

Example 2: create laravel migration

php artisan make:migration create_users_table

Example 3: laravel migration rollback

To rollback one step:

php artisan migrate:rollback

To rollback multiple steps:

php artisan migrate:rollback --step=[x]
  
To drop all tables and reload all migrations:

php artisan migrate:fresh

Example 4: laravel migration

php artisan migrate

Example 5: php artisan see last migration

php artisan migrate:status

Example 6: php artisan migration refresh

php artisan migrate:refresh --step=1

Tags:

C Example