Level 1
can you please print the result for $booking->services
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, I can't figure out why I'm getting this error message when I'm trying to load the service description from the booking. This is the error message I get: Property [description] does not exist on this collection instance.
BOOKING MODEL
public function services()
{
return $this->belongsToMany('App\Service');
}
FOREACH LOOP
@foreach ($bookingsGoingOut as $booking)
<tr>
<td class="border px-4 py-2">{{ optional($booking->services)->description }}</td>
</tr>
@endforeach
CONTROLLER
$bookingsGoingOut = Booking::where('pickUpDate', '=', $today)->with('services')->paginate(15);
As I can see you are using belongsToMany relationship which will give multiple services for a single booking
BOOKING MODEL
public function services()
{
return $this->belongsToMany('App\Service');
}
So I think you need to apply an extra iteration on $booking->services.
Like this
@foreach ($bookingsGoingOut as $booking)
@foreach (optional($booking->services) as $service)
<tr>
<td class="border px-4 py-2">{{ $service->description }}</td>
</tr>
@endforeach
@endforeach
Hope this will help you!
Please or to participate in this conversation.