In my laravel app I want to save histories of all actions (create,update,delete), to do this, I create histories
table and I observe all Models
and store in histories table on every event handled,
But, when I handle the User updated
event. for exemple, two row was saved on histories table, also when I handel the User created
event. even with ing
events.
N.B : I store user with create()
method and I update it with update()
method
UserObserver :
/**
* Handle the User "created" event.
*/
public function created(User $user): void
{
$user_resource =(new UserResource($user))->toResponse(request());
$history = History::create([
'model_type' => User::class,
'model_id' => $user->id,
'action' => 'create',
'action_date' => now(),
'model_object' => $user_resource->content(),
'user_id' => request()->user()->id,
]);
}
/**
* Handle the User "updated" event.
*/
public function updated(User $user): void
{
$user_resource =(new UserResource($user))->toResponse(request());
$history = History::create([
'model_type' => User::class,
'model_id' => $user->id,
'action' => 'update',
'action_date' => now(),
'model_object' => $user_resource->content(),
'user_id' => request()->user()->id,
]);
// dd($history);
}
When I delete this lines :
/**
* Handle the User "updated" event.
*/
no data was stored in histories table. I don't know the interst of this commented line and if thine lines handle the event twice
The issue is that I define the UserObserver
twice in EventServiceProvider