In this concise guide, we will illustrate the Laravel model updating event. If you're wondering how to utilize the updating event in Laravel, I'll provide a straightforward example with a solution. This article delves into the details of using the updating event in a Laravel model, offering a practical example of the Laravel updating event model.
In Laravel, model events offer a way to execute actions when a model is created, updated, deleted, and more. To illustrate the utilization of the updating
event in a Laravel model, consider the following example. Suppose you have a Post
model, and you wish to automatically trigger an event after updating a post record. This can be achieved using the updating
event.
Laravel Model Updating Event Example
I'll provide you with a straightforward example of how to invoke the updating
event from a model. Let's explore this simple illustration:
Create Post Model:
Create the Post
model using the Artisan command:
php artisan make:model Post
In the generated Post
model, 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 updated, the updating
event is triggered. We utilize a closure to generate a slug based on the title
attribute, employing the Str::slug
method from Laravel to create a URL-friendly slug from the title.
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, execute the following controller code, and you will observe the corresponding logs for the calling of the creating event and created event, as shown 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}
Thank you for reading this guide!