@Guardian I’m not sure what your question is. If you want to hook into model events, then do so? The documentation covers how to do so: https://laravel.com/docs/master/eloquent#events
Hook into Model events?
Dear All,
I am working on an application that has some models (obviously). Now I want to be able to hook into the default events provided by Laravel.
When looking at Model.php this should be
public function getObservableEvents()
{
return array_merge(
[
'creating', 'created', 'updating', 'updated',
'deleting', 'deleted', 'saving', 'saved',
'restoring', 'restored',
],
$this->observables
);
}
I have been googling around for this and have found 2 approaches.
- Hook directly in your model and fire events that you have mapped in the EventServiceProvider (read: http://stackoverflow.com/a/30540207)
- Create custom events (read: http://mydnic.be/post/easiest-way-to-bind-events-to-your-models) The latter feels less usefull to me since I would end up creating events that I feel I don't need. Since I just want to hook into the already existing CRUD events as defined above.
I would like to get some insights / opinions on this.
Many thanks in advance.
Ps: I am running L 5.2
Dear all,
After some playing around with all of this, here is how I implemented it for now.
I wanted to have an event that was fairly flexible, since I want to be able to log CRUD operations on various models, but I also want to be able to fire the log event in my controllers or elsewhere
I added this to the App\Providers\EventServiceProvider to register my event & listener
protected $listen = [
'App\Events\LogEvent' => [
'App\Listeners\LogListener',
],
];
I created my App\Events\LogEvent & App\Listeners\LogListener (php artisan make:listener LogListener --event=LogEvent) For now LogEvent only contains this, to be elaborated with referer, currently logged in user etc
public function __construct($message)
{
$this->message = $message;
}
LogListener
public function handle(LogEvent $event)
{
$log = new Log([
'type' => 'debug',
'message' => $event->message,
]);
$log->user_id = Sentinel::check() ? Sentinel::check()->id : null;
$log->save();
}
I created App\Providers\CrudLoggerProvider where I hooked into the "saving" event on the model, to log each time a model was created / updated and implemented some additional logic to validate if the user was created or updated
User::saving(function(User $user) {
$message = '';
foreach($user->getDirty() as $attribute => $value){
$original= $user->getOriginal($attribute);
if ($original != '') {
$message .= "Changed $attribute from '$original' to '$value'<br/>\r\n";
}
}
if($message == '') {
$message = 'User '.$user->last_name.' '.$user->first_name.' was created';
}
event(new LogEvent($message));
});
Finally, register your service provider in the 'providers' array in config/app.php like so
App\Providers\CrudLoggerProvider::class,
Just as an additional resource, I have implemented the basic logging in one of my controllers that sends a password reset link to the user like so
event(new LogEvent('Password reset requested for ' .$user->email));
I am very interested to gather some feedback or suggestions on improving this and I hope that this can serve as a resource to others.
Many thanks in advance.
Please or to participate in this conversation.