Laravel Observer depedencie injection gave a string type attribute instead of an integer.
I faced a problem with Laravel Observer because it gave me a collection with the wrong attribute type (integers become strings). I fixed this by calling the refresh() method. This is my code:
Observer:
class PaymentObserver implements ShouldHandleEventsAfterCommit
{
/**
* Handle the Payment "created" event.
*/
public function created(Payment $payment): void
{
$payment->refresh(); // need refresh or refacting data
$barbershop = Barbershop::find($payment->barbershop_id);
$barbershop->status = BarbershopStatusEnum::ACTIVE;
$barbershop->expired_date = Carbon::now()->addDays($payment->days_added);
$barbershop->save();
}
}
Filament Action: create new Payment
->action(function(Barbershop $barbershop, array $data){
try{
$data['user_id'] = Auth::user()->id;
$barbershop->payments()->create($data);
Notification::make()->title('Saved successfully')->success()->send();
}catch(Exception $e){
Notification::make()->title('Save Failed! ' . $e->getMessage())->danger()->send();
}
})
$payment before refresh():
enter image description here
$payment after refresh()
enter image description here
I am already trying to use attribute casting before fixing it with refresh(), and nothing happens. Perhaps this is a bug or just my mistake, let’s discuss.
Note:
- I use Laravel 11
- I use filament
Junaris Alf is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.