Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Eloquent: Laravel 7+ Foreign Keys


From Laravel 7, in migrations you don't need to write two lines for relationship field - one for the field and one for foreign key. Use method foreignId().

Before Laravel 7

Schema::table('posts', function (Blueprint $table)) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
}

From Laravel 7

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

Or, if your field is different from the table reference

Schema::table('posts', function (Blueprint $table)) {
$table->foreignId('created_by_id')->references('id')->on('users');
}

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon