laravel migration field types 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: laravel migration foreign key 5.6

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

    $table->foreign('user_id')->references('id')->on('users');
});

Example 3: laravel migration integer

$table->bigInteger('votes');

Example 4: laravel before migration

Schema::table('users', function ($table) {
    $table->string('email')->after('id')->nullable();
});

Example 5: laravel field types from database field type

// in laravel if error of missing pdomysql driver issue the command
// composer require doctrine/dbal
DB::connection()->getDoctrineColumn($table_name, $field_name)->getType()->getName()

Example 6: create migration laravel

php artisan make:Model Product -m -c  --resource

Tags:

Misc Example