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

add foreign key column laravel 5.8

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');
        });
    }
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 8 foreign key migration

use IlluminateDatabaseSchemaBlueprint;
use IlluminateSupportFacadesSchema;

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

    $table->foreign('user_id')->references('id')->on('users');
});
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

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

PREVIOUS NEXT
Code Example
Php :: insert batch in laravel 
Php :: wp add menu page and subpage 
Php :: Genrate Random Integer 10 digits in php 
Php :: https://www.60d48b1061adf.site123/wp-login.php 
Php :: wp php category page count products 
Php :: add a controller method in laravel routes 
Php :: blank admin page magento 2.3 
Php :: curl error (code 3) url malformed laravel 
Php :: php url parameters 
Php :: rawbetween in laravel 
Php :: one lin if statement php 
Php :: laravel relationship search 
Php :: laravel valet refresh env 
Php :: Laravel migrations custom foreign key 
Php :: php stop loading page 
Php :: laravel 8 add column to existing table 
Php :: php self referencing form 
Php :: db seed in controller 
Php :: php print object 
Php :: laravel reload relationship 
Php :: Remove prefix on category title 
Php :: laravel model uploaded file name 
Php :: php prepared statement upload file 
Php :: php mail template 
Php :: laravel include config 
Php :: php command get ini params 
Php :: laravel invoice toturial 
Php :: php pdo 
Php :: php validate credit card expiration date 
Php :: php json_encode float 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =