I need to generate a function to call after or before save() or update() but i don't know how to do. I think I need a callback from save() update() but I don't know how to do. Thanks
3 Answers
Inside your model, you can add a boot() method which will allow you to manage these events.
For example, having User.php model:
class User extends Model
{
public static function boot()
{
parent::boot();
self::creating(function($model){
// ... code here
});
self::created(function($model){
// ... code here
});
self::updating(function($model){
// ... code here
});
self::updated(function($model){
// ... code here
});
self::deleting(function($model){
// ... code here
});
self::deleted(function($model){
// ... code here
});
}
}
You can review all available events over here: https://laravel.com/docs/5.2/eloquent#events
-
11Be aware: "When issuing a mass update via Eloquent, the saved and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update." laravel.com/docs/5.8/eloquent#events– FizkJun 18, 2019 at 6:16
-
1You're absolutely right. This is only when running single requests, when working with batches I recommend to loop through the batch in case you need these events triggered. Jun 19, 2019 at 18:41
-
1A quick useful additional note @MauroCasas : You better use booted method instead of boot. This method stands for this specific situation and called right after boot(). Doing so, you don't need to call parent::boot() Feb 9, 2021 at 23:47
This only works after an event happened on your model.
Method 1, using Observers
Create an observer for your model
php artisan make:observer UserObserver --model=User
this will create an event observer on your model
class UserObserver
{
/**
* Handle the User "created" event.
*
* @param \App\Models\User $user
* @return void
*/
public function created(User $user)
{
//
}
/**
* Handle the User "updated" event.
*
* @param \App\Models\User $user
* @return void
*/
public function updated(User $user)
{
//
}
/**
* Handle the User "deleted" event.
*
* @param \App\Models\User $user
* @return void
*/
public function deleted(User $user)
{
//
}
/**
* Handle the User "forceDeleted" event.
*
* @param \App\Models\User $user
* @return void
*/
public function forceDeleted(User $user)
{
//
}
}
You must register this observer in the boot method on one of your ServiceProviders preferably the AppServiceProvider
// App\Providers\AppServiceProvider.php
public function boot()
{
User::observe(UserObserver::class);
}
Method 2, using Closures
You can register custom events in the static booted method of your model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The "booted" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::created(function ($user) {
//
});
}
}
Available observable events
// Illuminate\Database\Eloquent\Concerns
/**
* Get the observable event names.
*
* @return array
*/
public function getObservableEvents()
{
return array_merge(
[
'retrieved', 'creating', 'created', 'updating', 'updated',
'saving', 'saved', 'restoring', 'restored', 'replicating',
'deleting', 'deleted', 'forceDeleted', 'trashed'
],
$this->observables
);
}
Note from Laravel documentation
When issuing a mass update via Eloquent, the saving, saved, updating, and updated model events will not be fired for the updated models. This is because the models are never actually retrieved when issuing a mass update.
Create a provider by using this command
php artisan make:provider ProviderClassName
then define the callbacks for models in boot function
Model::created(function($model){
//Do you want to do
});
List of available callbacks:
Model::creating(function($model){});
Model::updated(function($model){});
Model::updating(function($model){});
Model::deleted(function($model){});
Model::deleting(function($model){});
Model::saving(function($model){});
Model::saved(function($model){});
-
2
-
No need for a complete new ServiceProvider, you can use AppServiceProvider which is used for Application customization. Jul 31, 2016 at 16:23
-
what is a difference when I add this function to register function instead of boot? Aug 8, 2019 at 8:28
-
There is also
retrieved()
,restoring()
,restored()
,replicating()
andforceDeleted()
– MacroManAug 19, 2019 at 13:29