Hello Devs, 

In this tutorial, we will learn Laravel Model Deleting Event Example

deleting method sent before records are deleted or soft-deleted. These are the events that you can use with your Laravel models.

Follow this step-by-step guide below. 


Step 1: Create Item Event:

php artisan make:event ItemEvent


app/Events/ItemEvent.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use App\Item;
use Log;

class ItemEvent
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct()
    {

    }

    /**
     * Get the channels the event should broadcast on.
     *
     * @return \Illuminate\Broadcasting\Channel|array
     */
    public function itemDeleted(Item $item)
    {
        Log::info("Item Deleted Event Fire: ".$item);
    }
}


Step 2: Register Event

app/Providers/EventServiceProvider.php

<?php

namespace App\Providers;
use Illuminate\Support\Facades\Event;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{

    /**
     * The event listener mappings for the application.
     *
     * @var array
     */

    protected $listen = [

        'App\Events\Event' => [
            'App\Listeners\EventListener',
        ],
        'item.deleted' => [
            'App\Events\ItemEvent@itemDeleted',
        ]
    ];

    /**
     * Register any events for your application.
     *
     * @return void
     */

    public function boot()
    {
        parent::boot();
    }
}


Step 3: Create Model

app/Item.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{

    protected $fillable = ['name', 'price'];

    public static function boot() {

	    parent::boot();

	    static::deleted(function($item) {
	        \Log::info('Item Deleted Event:'.$item);
	    });
	    static::deleting(function($item) {
	        \Log::info('Item Deleting Event:'.$item);
	    });
	    
	}
}


Step 4: Add Demo Route

routes/web.php

Route::get('item-deleted', function(){
    \App\Item::find(1)->delete();
    dd('Item deleted successfully.');
});

run following example like as bellow :

http://localhost:8000/item-deleted

find in your log file like as bellow:

storage/logs/laravel.log

[2020-09-02 12:48:03] local.INFO: Item Deleting Event:{"id":1,"name":"demo","price":"300","date":"2020-09-01","deleted_at":null,"created_at":"2020-09-01T13:04:22.000000Z","updated_at":"2020-09-01T13:04:22.000000Z"}  
[2020-09-02 12:48:03] local.INFO: Item Deleted Event:{"id":1,"name":"demo","price":"300","date":"2020-09-01","deleted_at":"2020-09-02T12:48:03.000000Z","created_at":"2020-09-01T13:04:22.000000Z","updated_at":"2020-09-02T12:48:03.000000Z"}  


May this example help you.