1$table->unsignedBigInteger('user_id');
2$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
1Schema::table('posts', function (Blueprint $table) {
2 $table->unsignedBigInteger('user_id');
3
4 $table->foreign('user_id')->references('id')->on('users');
5});
6OR
7Schema::table('posts', function (Blueprint $table) {
8 $table->foreignId('user_id')->constrained();
9});
1To rollback one step:
2
3php artisan migrate:rollback
4
5To rollback multiple steps:
6
7php artisan migrate:rollback --step=[x]
8
9To drop all tables and reload all migrations:
10
11php artisan migrate:fresh
1Schema::table('posts', function (Blueprint $table) {
2 $table->unsignedBigInteger('user_id');
3
4 $table->foreign('user_id')->references('id')->on('users');
5});
1use Illuminate\Database\Schema\Blueprint;
2use Illuminate\Support\Facades\Schema;
3
4Schema::table('posts', function (Blueprint $table) {
5 $table->unsignedBigInteger('user_id');
6
7 $table->foreign('user_id')->references('id')->on('users');
8});
1$table->foreignId('post_id')
2 ->constrained()
3 ->onUpdate('cascade')
4 ->onDelete('cascade');