lundi 29 octobre 2018

laravel observer cannot get data from related model

I am facing a problem while saving data through an observer. Any help from the community will be greatly appreciated. My observer is as follow.

public function created(Reservation $reservation)
{
    $from = Carbon::parse($reservation->checkin);
    $to = Carbon::parse($reservation->checkout);
    $diff_in_days = $to->diffInDays($from);

    $total_price = $reservation->rooms->sum('price') * $diff_in_days;
    dd($total_price);
}

The above code snippet perfectly works in controller, but somehow it's not working in the observer. It gets a 0 value, besides that.

$reservation->rooms;

gets an empty array, while

$reservation->rooms();

it also get belongsToMany relationship with attributes and original are empty arrays : []



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire