Search
 
SCRIPT & CODE EXAMPLE
 

PHP

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();
});
Comment

foreign key in Laravel

// one_line code for foreign in laravel
$table->foreignId('user_id')->constrained()->onDelete('cascade');
Comment

laravel foreign key constraint

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();
    });
}
Comment

laravel foreign key

//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');
Comment

foreign key laravel migration

$table->foreign('column_name')->references('id')->on('table_name')->onDelete('cascade');
Comment

laravel 8 foreign key

Schema::table('posts', function (Blueprint $table) {
    $table->foreignId('user_id')->constrained();
});
Comment

laravel foreign key

$table->foreignId('user_id')
      ->constrained()
      ->onUpdate('cascade')
      ->onDelete('cascade');
Comment

laravel foreign

$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')
Comment

foreign key in laravel 9

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');
Comment

How to create foreign key in Laravel

$table->foreign('category_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
Comment

foreign key string laravel

Schema::table('portfolios', function (Blueprint $table) {
            $table->string('filter_alias');
            $table->foreign('filter_alias')->cascadeOnDelete()->references('alias')->on('filters');
        });
Comment

PREVIOUS NEXT
Code Example
Php :: root composer.json requires php ^7.3 but your php version (8.0.3) does not satisfy that requirement. 
Php :: php - How do I calculate the percentage of a number? 
Php :: magento 2 db connection 
Php :: add controller to laravel with requests 
Php :: curl post laravel 
Php :: laravel merge two query builder 
Php :: upload multiple images in php 
Php :: declare empty array in php 
Php :: laravel log reader 
Php :: laravel form 
Php :: wordpress reserved image size name 
Php :: how to data save usigng request all laravel 
Php :: php array get value at index 
Php :: create multiple session in php 
Php :: php proper function comments 
Php :: php excel to array 
Php :: merge two objects php laravel 
Php :: enqueue css 
Php :: Custom Font Laravel 
Php :: while true php 
Php :: php send json post 
Php :: Laravel eloquent permanent soft delete 
Php :: laravel count distance lat/longtidue 
Php :: today date to ago for the date in php 
Php :: excerpt with Laravel 
Php :: phpunit test private function 
Php :: add another column in a table in laravel 
Php :: wp_debug 
Php :: real time update using ajax php 
Php :: javascript function in php json_encode 
ADD CONTENT
Topic
Content
Source link
Name
9+4 =