Laravel Model Events

I will explain step-by-step tutorial laravel model events example. I would like to show you model events in laravel. We will look at examples of eloquent model events in laravel. I’m going to show you about the laravel model events created.

I will give you a simple example of eloquent model events of laravel and you can also use it in laravel 6, laravel 7, and laravel 8 version.

Laravel provides a list of eloquent model events and each model event has its 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.

Sometimes, we need to add a unique number or create a slug from the title than before creating a record you need to update these fields at a time we can use those times of events. the same thing when you update records then also you have to update slug and might when removing records then also you have to delete child records. so the event is very helpful and I will say you must have used it when you need this type of situation.

I will explain few important events with examples so you will understand how it works and how you can use it.

In this example, I will write down creating, created, updating, updated and deleted events and I will show you one by one example with output in the log file so you can easily understand, how model events work.

app/Models/Product.php

<?phpnamespace 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);});}}

app/Http/Controllers/ProductController.php

<?phpnamespace 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');}}

Output Log File:

[2020-10-20 14:37:26] local.INFO: Creating event call: {"name":"silver","detail":"This is silver"}[2020-10-20 14:37:26] local.INFO: Created event call: {"name":"silver","detail":"This is silver","slug":"silver","updated_at":"2020-10-20T14:37:26.000000Z","created_at":"2020-10-20T14:37:26.000000Z","id":5}

Update Record: Updating and Updated Event

app/Http/Controllers/ProductController.php

<?phpnamespace 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');}}

Output Log File:

[2020-10-20 14:39:04] local.INFO: Updating event call: {"id":5,"name":"silver updated","detail":"This is silver","created_at":"2020-10-20T14:37:26.000000Z","updated_at":"2020-10-20T14:37:26.000000Z","slug":"silver"}[2020-10-20 14:39:04] local.INFO: Updated event call: {"id":5,"name":"silver updated","detail":"This is silver","created_at":"2020-10-20T14:37:26.000000Z","updated_at":"2020-10-20T14:39:04.000000Z","slug":"silver-updated"}

app/Http/Controllers/ProductController.php

<?phpnamespace 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');}}

Output Log File:

[2020-10-21 03:14:45] local.INFO: Deleted event call: {"id":5,"name":"silver updated","detail":"This is silver","created_at":"2020-10-20T14:37:26.000000Z","updated_at":"2020-10-20T14:39:04.000000Z","slug":"silver-updated"}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store