DEV Community

Rafael França
Rafael França

Posted on • Originally published at Medium

Cascading on update (and on delete) in migration

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

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


Enter fullscreen mode Exit fullscreen mode

Let me explain the changes:

  • id() it's the same of bigIncrements('id'). If you want to change the column name from id to cid, for eg., you can use id('cid');
  • foreignId('author') it's the same of unsignedBigInteger('author');
  • constrained('users') it's the same of references('id')->on('users'), that by default references the id

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)

Collapse
 
vivaldidev profile image
vivaldi-dev

Very usefull
Thank you