Laravel 10 event handle runes two times.
$reference = "ABCD1234";
event(new AppEventsRewardOnBooking($reference));
when i fire this event then every thing is fine but it’s handle function fires 2 times.
this is how i register this in EventServiceProvider class
protected $listen = [
// ...........
// ..............
'AppEventsRewardOnBooking' => [
'AppListenersAddCredit',
],
];
In the RewardOnBooking class I’m just doing this nothing else not even broadcastOn() somthing –
public function __construct($reference)
{
$this->reference = $reference;
}
and in the AddCredit class nothing to construct, i’m just handle
public function handle(RewardOnBooking $event): void
{
$userCredit = Credits::where('user_id', '=', Auth::user()->id)->orderBy('id', 'desc')->first();
if ($userCredit == null) {
Credits::firstOrCreate(
['user_id' => Auth::user()->id],
[
'creditsAmounts' => 0,
"description" => 'added credit for your first booking',
]
)->first();
} else {
$amount = rand(1, 5);
Credits::create([
'user_id' => Auth::user()->id,
"creditsAmounts" => $userCredit['creditsAmounts'] + $amount,
"description" => 'Added credit for your booking reference - ' . $event->reference,
'responce' => $amount,
]);
}
}
I am trying to do when this event is happened then add some random credit amount in user Credits but this i is happened two times
This is what stored in DB
but i just want to do this ones.
You can see this DB recodes
Aimless is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
8