Cascading on update (and on delete) in migration
Tags: laravel, eloquent, protips, php
Published: 2020-07-26 00:19:04

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 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.

{% medium https://medium.com/@rafael_franca/laravel-tip-cascading-on-update-in-migration-2100af33081 %}