Friday 1 February 2019

Laravel migrations to DB

create migration:
php artisan make:migration create_users_table

execute all migration(on migration file date order):
php artisan migrate

execute single migration:
php artisan migrate --path=/database/migrations/my_migration.php

-----------------------------------------------------
Laravel SQL DB Knowledge
-----------------------------------------------------

foregin key both column has to be same type and length (int(11) -> int(11) ), int(10) is only for unsigned ones
$table->integer('user_id')->unsigned();

laravel change table column requires plugin:
composer require doctrine/dbal

You should create a new migration file let's say 'add_user_foreign_key.php'
public function up()
{
    Schema::table('lists', function(Blueprint $table)
    {
         $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('lists', function(Blueprint $table)
    {
    $table->dropForeign('user_id'); //
    });
}  
The run
 php artisan migrate



No comments:

Post a Comment