Laravel migration

  • During relational database you must write the migration for parent table first and then child table.
  • Foreign key:
    $table->integer('user_id')->unsigned()->nullable();
    $table->foreign('user_id')->references('id')->on('users');
  • Write foreign key outside of create and inside of table.
    Schema::create('articles', function (Blueprint $table) {
        $table->increments('id');
        $table->string('body');
        $table->timestamps();
    });
    // Add Foreign key
    Schema::table('articles', function (Blueprint $table) {
        $table->unsignedInteger('user_id');
        $table->foreign('user_id')->references('id')- 
       >on('users')->onDelete('cascade');
    });

Add a column to an existing table

for Laravel 5+:
php artisan make:migration add_paid_to_users_table --table=users
You then need to use the Schema::table() method (as you're accessing an existing table, not creating a new one). And you can add a column like this:
public function up()
{
    Schema::table('users', function($table) {
        $table->integer('paid');
    });
}
and don't forget to add the rollback option:
public function down()
{
    Schema::table('users', function($table) {
        $table->dropColumn('paid');
    });
}
Then you can run your migrations:
php artisan migrate
This is all.

Labels: ,

© copyright-2020 Rejaul