That Laravel’s documentation is rich in content no one can deny, but it does not mean that it has everything it can offer us.
An example is when we need to perform a cascading update, some indicate the use of static methods in the model. But, there is a simpler way that few developers know, and is not explicit in the documentation.
On update
An example how our code would look like:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author')->unsigned();
$table->timestamps();
$table->foreign('author')->references('id')->on('users')->onUpdate('cascade');
});
Now, our cascading on update it’s in the migration.
On delete
If I want to cascade on delete?
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author')->unsigned();
$table->timestamps();
$table->foreign('author')->references('id')->on('users')->onDelete('cascade');
});
It's easy, right? Let's avance.
Combine On update and On delete
If, I want to use on update and on delete in the same column? Yes, you can!
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('author')->unsigned();
$table->timestamps();
$table->foreign('author')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
});
Changes on Laravel 7
In your 7th version, Laravel bring us some new use case:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->foreignId('author')->constrained('users')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
Let me explain the changes:
-
id()
it's the same ofbigIncrements('id')
. If you want to change the column name fromid
tocid
, for eg., you can useid('cid')
; -
foreignId('author')
it's the same ofunsignedBigInteger('author')
; -
constrained('users')
it's the same ofreferences('id')->on('users')
, that by default references theid
Advanced
By default, constrained()
references the id
, but you can change this, passing a second param, for eg., constrained('users', 'cid')
, in this case, it's the same of references('cid')->on('users')
.
More advanced
By default, constrained()
not need the first param, if you use the default structure of database (tables name in plural and in english), and your foreignId()
is like this foreignId('user_id')
, you can use only $table->foreignId('author')->constrained()
.
From behind, Laravel get the your foreignId
that's user_id
, and select all before the last undeline, in our eg, user
, and apply the plural, that convert in users
, it's the name of table. And, all after last underline will be the name of the column.
In the end, foreignId('user_id')->constrained()
, it's the same of foreign('user_id')->references('id')->on('users')
, even as foreignId('user_cid')->constrained()
, it's the same of foreign('user_cid')->references('cid')->on('users')
.
What do you think about this tip?
Soon I back with more! :)
PS: Originally posted on my account on Medium.
Top comments (1)
Very usefull
Thank you