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();
});
// one_line code for foreign in laravel
$table->foreignId('user_id')->constrained()->onDelete('cascade');
public function up()
{
Schema::create('replies', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('body');
$table->unsignedBigInteger('question_id');
$table->integer('user_id')->unsigned();
$table->foreign('question_id')->references('id')->on('questions')->onDelete('cascade');
$table->timestamps();
});
}
//acording to laravel 7>=
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('user_id')->constrained();
});
//for deffirent table
$table->foreignId('user_id')->constrained('users');
//for taking action
$table->foreignId('user_id')
->constrained()
->onUpdate('cascade')
->onDelete('cascade');
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('user_id')->constrained();
});
$table->foreignId('user_id')
->constrained("users") <- // You don't need to specify table if it matched laravel naming conventions.
->onUpdate('cascade')
->onDelete('cascade');
$table->foreignId('user_id')
->constrained()
->onUpdate('cascade')
->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
Schema::table('portfolios', function (Blueprint $table) {
$table->string('filter_alias');
$table->foreign('filter_alias')->cascadeOnDelete()->references('alias')->on('filters');
});