Laravel Model Deleting Event Example

10-Apr-2023

.

Admin

Laravel Model Deleting Event Example

Hi Guys,

In this example,I will learn you how to use model deleting event in laravel.you can easy and simply use model deleting event in laravel.

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

Models events are simpy hooks into the important points of a model's lifecycle which you can use to easily run code when database records are deleting.

Step 1: Create Item Event


In this step we will create event for Item model. So simply run bellow command and create event for Item.

php artisan make:event ItemEvent

Ok, now you have new file in Events folder. So you can see ItemEvent.php file in your Events directory and put bellow code on that file.

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

Now, we have to register following events on EventServiceProvider.php file. So let's simply open that file and put like as bellow code:

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

Now we will create morel for "items" table. So just simple create Item.php file and also add events code on boot method:

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

Now we will test following event like delete model task. So just simple add following routes:

routes/web.php

Route::get('item-deleted', function(){

\App\Item::find(1)->delete();

dd('Item deleted successfully.');

});

Ok, now we are ready to run following example like as bellow :

http://localhost:8000/item-deleted

Then you will 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"}

It will help you..

#Laravel 7

#Laravel

#Laravel 6