Creating laravel foreign keys
This is a tiny tip for more experienced Laravel developers. Laravel developers who have been working with the framework for a long time that might have missed this small, but nice, migrations improvement.
In the past, to set a foregin key you first had to add an unsignedBigInteger
with the name of the column, and then, typically at the end of the callback, you would use the blueprint foreign()
method, pass it the column name, and then reference the id on the other table.
1Schema::create('events', function (Blueprint $table) { 2 $table->id(); 3 $table->unsignedBigInteger('user_id'); 4 $table->unsignedBigInteger('person_id'); 5 $table->string('name'); 6 $table->date('date'); 7 $table->string('repeat_every'); 8 $table->string('notify_days_before'); 9 $table->timestamps();10 11 $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');12 $table->foreign('person_id')->references('id')->on('people')->onDelete('cascade');13});
However, in newer versions of Laravel, there is a better way to do this.
Instead of unsignedBigInteger
, we can do:
1Schema::create('events', function (Blueprint $table) { 2 $table->id(); 3 $table->foreignId('user_id')->constrained()->onDelete('cascade'); 4 $table->foreignId('person_id')->constrained()->onDelete('cascade'); 5 $table->string('name'); 6 $table->date('date'); 7 $table->string('repeat_every'); 8 $table->string('notify_days_before'); 9 $table->timestamps();10});
Now, the constrained()
method will use conventions to determine the table and column name being referenced, but if your table name somehow doesn’t match, you can pass it as argument.
1Schema::create('events', function (Blueprint $table) { 2 $table->id(); 3 $table->foreignId('user_id')->constrained('users')->onDelete('cascade'); 4 $table->foreignId('person_id')->constrained('people')->onDelete('cascade'); 5 $table->string('name'); 6 $table->date('date'); 7 $table->string('repeat_every'); 8 $table->string('notify_days_before'); 9 $table->timestamps();10});
That’s it.
Usually, I don’t get that excited by syntactic sugar, but this is significantly better.
Not only because it’s shorter and more expressive, but because it also makes code easier to delete - and code that’s easy to delete is always better.
This was tiny tip #2 - I hope you enjoyed it.
-
1Submit form on textarea enter [VueJS]02:10
-
Creating laravel foreign keys01:28
-
3Customizing validation error messages in Laravel03:05
-
4Use scoped bindings to avoid extra guards in your Laravel controllers - Tiny tip #402:27
-
5Optimizing inertia js responses in Laravel - Tiny Tip #502:27
-
6Install prettier and enforce it in your projects08:13
-
7Preventing avatar background bleed - TailwindCSS02:33
-
8Creating a simple Golang CLI app12:30
-
9Fast screenshots with Chrome Dev Tools01:54
-
1Submit form on textarea enter [VueJS]02:10
-
Creating laravel foreign keys01:28
-
3Customizing validation error messages in Laravel03:05
-
4Use scoped bindings to avoid extra guards in your Laravel controllers - Tiny tip #402:27
-
5Optimizing inertia js responses in Laravel - Tiny Tip #502:27
-
6Install prettier and enforce it in your projects08:13
-
7Preventing avatar background bleed - TailwindCSS02:33
-
8Creating a simple Golang CLI app12:30
-
9Fast screenshots with Chrome Dev Tools01:54