Hello Geek, I will walk you through a step-by-step tutorial laravel model events example. I’d like to demonstrate model events in Laravel. We’ll take a look at some examples of eloquent model events in Laravel. I’m going to show you how to create laravel model events.
I will give you a simple example of eloquent model events in Laravel, which you can also use in Laravel 6, 7, 8, and 9.
How To Create Laravel Model Events Tutorial With Example
Laravel provide list of eloquent model events and each model event have it’s own function. it’s very helpful. i love laravel eloquent model events.
- creating: Call Before Create Record.
- created: Call After Created Record.
- updating: Call Before Update Record.
- updated: Class After Updated Record.
- deleting: Call Before Delete Record.
- deleted: Call After Deleted Record.
- retrieved: Call Retrieve Data from Database.
- saving: Call Before Creating or Updating Record.
- saved: Call After Created or Updated Record.
- restoring: Call Before Restore Record.
- restored: Call After Restore Record.
- replicating: Call on replicate Record.
When we need to add a unique number or create a slug from a title, we must update these fields before creating a record so that we can use those times of event. When you update records, you must also update the slug, and if you remove records, you must also delete the child records. So the event is very useful, and I would recommend that you use it whenever you are in this type of situation.
I’ll explain a few key events with examples so you can see how it works and how you can use it.
In this example, I will write down creating, creating, updating, updating, and deleting events and show you each one with output in a log file so you can easily understand how model events work.
laravel model events
Create Product Model with events
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Log;
use Str;
class Product extends Model
{
use HasFactory;
protected $fillable = [
'name', 'slug', 'detail'
];
/**
* Write code on Method
*
* @return response()
*/
public static function boot() {
parent::boot();
/**
* Write code on Method
*
* @return response()
*/
static::creating(function($item) {
Log::info('Creating event call: '.$item);
$item->slug = Str::slug($item->name);
});
/**
* Write code on Method
*
* @return response()
*/
static::created(function($item) {
/*
Write Logic Here
*/
Log::info('Created event call: '.$item);
});
/**
* Write code on Method
*
* @return response()
*/
static::updating(function($item) {
Log::info('Updating event call: '.$item);
$item->slug = Str::slug($item->name);
});
/**
* Write code on Method
*
* @return response()
*/
static::updated(function($item) {
/*
Write Logic Here
*/
Log::info('Updated event call: '.$item);
});
/**
* Write code on Method
*
* @return response()
*/
static::deleted(function($item) {
Log::info('Deleted event call: '.$item);
});
}
}
Now, we’ll simply call one model method at a time and see what happens:
Create Record: Creating and Created Event
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
Product::create([
'name' => 'silver',
'detail' => 'This is silver'
]);
dd('done');
}
}
Update Record: Updating and Updated Event
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
Product::find(5)->update([
'name' => 'silver updated',
'detail' => 'This is silver'
]);
dd('done');
}
}
Delete Record: Deleted Event
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
Product::find(5)->delete();
dd('done');
}
}
All the best nerd!