In this tutorial you will learn about the How to use Model Events in Laravel 8 and its application with practical example.
In this How to use Model Events in Laravel 8 tutorial I will show you how laravel model event works and how to use model event. In this tutorial you will learn to use model events in laravel 8 application. You will also learn about model events in laravel 8. In this article example of eloquent model events laravel 8. I will also provide you a list of laravel eloquent model events and each model event have it’s own function. In laravel each of the eloquent model events have it’s own function.
- 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.
How to use Model Events in Laravel 8
In this step by step tutorial I will show you how to use Model Events in Laravel 8. Please follow the instruction given below:
Create Product Model with events
First of all we will create a product model with events. Lets open create and open product model file and put the following code in it:
app/Models/Product.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<?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); }); } } |
Creating and Created Event
app/Http/Controllers/ProductController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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'); } } |
Please view the log file to check output
Updating and Updated Event
app/Http/Controllers/ProductController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
<?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'); } } |
Please view the log file to check output
Delete Record: Deleted Event
app/Http/Controllers/ProductController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?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'); } } |
Please view the log file to check output