Laravel Model Events List; In this tutorial we will sho you laravel model events list tutorial with example step by step. Many times we need to save, create, delete any data to before or after the event is generate. Laravel Model events allow you to tap into various points in a model’s lifecycle, and can even prevent a save or delete from happening.
The Laravel model events documentation outlines how you can hook into these events with event classes, but this article aims to build upon and fill in a few additional details on setting up events and listeners.
List of Laravel Model Events
Laravel Eloquent has many events that you can hook into and add custom functionality to your models. Many time we need to some column update or create with data before or after we save the data, or delete any else, wo we need to model events. One of the features of Eloquent is the implementation of the observer pattern for sending and listening to events sent by Laravel models when actions such as creating or saving models are executed. But we will explain the laravel inbuild observer here with model evetns. Let’s see Laravel model events tutorial with example or you can use any other laravel version easily.
Here is the full list of laravel model evetns:
- creating: call before creating a record
- created: Call after creating a record
- saving: Call before creating or updating a record
- updating: Call before updating a record
- updated: Class after updating a record
- deleting: Call before deleting a record
- deleted: Call after deleting a record
- retrieved: Call after getting data from the database
- saved: Call after creating or updating a record
- restoring: Call before restoring a record
- restored: Call after restoring a record
- replicating: Call on replicate record
Define Events in Model
Here we can define any events in our model just like below, Here we have added events in product model, You can use your requirements.
app/Models/Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use Log;
class Product extends Model
{
use HasFactory;
protected $fillable = [
'name', 'slug', 'description'
];
/**
* 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(Str::lower(str_replace(' ', '_', $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(Str::lower(str_replace('', '_', $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);
});
}
}
Usage of Model Events
Laravel event automatically generate when we call them by create, update, delete methods etc.
Creating and Created Event Example:
Here the below creating and created events call before produt create and after product created.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class ProductController extends Controller
{
public function store(Request $request)
{
Product::create([
'name' => 'Test Product',
'description' => 'This is test product'
]);
return redirect()->route('products.index')->with('success','Product created successfully.');
}
}
Log File Output:
You can check the output in your log file, the output look like this.
[2020-10-20 14:37:26] local.INFO: Creating event call: {"name":"Test Product","description":"This is test product"}
[2020-10-20 14:37:26] local.INFO: Created event call: {"name":"Test Product","description":"This is test product","slug":"test-product","updated_at":"2020-10-20T14:37:26.000000Z","created_at":"2020-10-20T14:37:26.000000Z","id":5}
Updating and Updated Event Example:
Same as creating and created the updating and updated events work.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class ProductController extends Controller
{
public function update(Request $request, Product $product)
{
Product::find(1)->update([
'name' => 'Test Product Updated',
'detail' => 'This is test product now updated'
]);
return redirect()->route('products.index')->with('success','Product updated successfully');
}
}
Here the output of updating and updated model events look like:
[2020-10-20 14:37:26] local.INFO: Creating event call: {"name":"Test Product Updated","description":"This is test product","slug":"test-product"}
[2020-10-20 14:39:04] local.INFO: Updated event call: {"id":5,"name":"Test Product Updated","description":"This is test product now updated","created_at":"2020-10-20T14:37:26.000000Z","updated_at":"2020-10-20T14:39:04.000000Z","slug":"test-product-updated"}
Deleted Event:
You can use deleting and deleted as well.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Product;
class ProductController extends Controller
{
public function destroy(Product $product)
{
$product->delete();
return redirect()->route('products.index')
->with('success','products deleted successfully');
}
}
Here the output of deleted model events:
[2020-10-21 03:14:45] local.INFO: Deleted event call: {"id":5,"name":"Test Product","detail":"This is test product","created_at":"2020-10-20T14:37:26.000000Z","updated_at":"2020-10-20T14:39:04.000000Z","slug":"test-product-updated"}
I hope you enjoy more with the laravel model events tutorial…
1 thought on “Laravel Model Events Tutorial with Example”