Laravel Model Updating Event Example

By Hardik Savani November 5, 2023 Category : Laravel

Hey Folks,

In this short guide, we will show you laravel model updating event. If you have a question about how to use model updating event in laravel then I will give a simple example with a solution. This article goes in detailed on how to use updating event in laravel model. We will look at an example of laravel updating event model.

In Laravel, you can use model events to perform actions when a model is being created, updated, deleted, etc. To demonstrate how to use the `updating` event in a Laravel model, I'll provide you with an example. Let's assume you have a `Post` model, and you want to automatically call event after updating post record. You can do this using the `updating` event.

I will give you simple example of how to call updating event in from model. so, let's see the very simple example:

Create Post Model:

1. Create the `Post` model using the Artisan command:

php artisan make:model Post

2. In the generated `Post` model, you can define the `updating` event using the `static::updating` method. Here's an example:

app/Models/Post.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use Illuminate\Support\Str;

class Post extends Model

{

protected $fillable = ['title', 'content', 'slug'];

/**

* Write code on Method

*

* @return response()

*/

protected static function boot()

{

parent::boot();

static::updating(function ($post) {

info('Updating event call: '.$post);

$post->slug = Str::slug($post->title);

});

static::updated(function ($post) {

info('Updated event call: '.$post);

});

}

}

Create Post Record:

In this example, when a new `Post` model is being created, the `updating` event is triggered, and we use a closure to generate a slug based on the `title` attribute. We're using the `Str::slug` method from Laravel to create a URL-friendly slug from the title.

3. Now, when you create a new `Post` instance and save it, the `updating` event will automatically set the slug for you. Here's how you can create a new post:

app/Http/Controllers/PostController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$post = Post::find(1);

$post->title = "This is Title".

$post->body = "This is Content".

$post->save();

dd($post);

}

}

Now, you can run following controller code and you will find following log for calling creating event and created event as like the below:

Output:

[2023-10-20 14:37:26] local.INFO: Updating event call: {"title":"This is Title","content":"This is a Content","slug":"this-is-

testing","updated_at":"2023-10-20T14:37:26.000000Z","created_at":"2023-10-20T14:37:26.000000Z","id":1}

[2023-10-20 14:37:26] local.INFO: Updated event call: {"title":"This is Title","content":"This is a Content","slug":"this-is-

title","updated_at":"2023-10-20T14:37:26.000000Z","created_at":"2023-10-20T14:37:26.000000Z","id":1}

I hope it can help you...

Tags :
Shares