xxxxxxxxxx
$table->foreignId('account_id')->index()->constrained()->cascadeOnDelete();
xxxxxxxxxx
update your `integer('user_id')` to `bigInteger('user_id')`
public function up() {
Schema::create('evaluation', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('user_id')->unsigned()->index();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
xxxxxxxxxx
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
xxxxxxxxxx
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
xxxxxxxxxx
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
Schema::table('posts', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
xxxxxxxxxx
$table->foreign('column_name')->references('id')->on('table_name')->onDelete('cascade');
xxxxxxxxxx
$table->foreignId('post_id')
->constrained()
->onUpdate('cascade')
->onDelete('cascade');
xxxxxxxxxx
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('user_id')->constrained();
});
xxxxxxxxxx
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddForeignKeyToTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('your_table_name', function (Blueprint $table) {
$table->foreign('foreign_key_column')->references('referenced_column')->on('referenced_table')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('your_table_name', function (Blueprint $table) {
$table->dropForeign(['foreign_key_column']);
});
}
}
xxxxxxxxxx
Firstly you have to make your user_id field an index:
$table->index('user_id');
After that you can create a foreign key with an action on cascade:
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
If you want to do that with a new migration, you have to remove the index and foreign key firstly and do everything from scratch.
On down() function you have to do this and then on up() do what I've wrote above:
$table->dropForeign('lists_user_id_foreign');
$table->dropIndex('lists_user_id_index');
$table->dropColumn('user_id');