hjortur17
1 month ago

Laravel blade doesn't render all from foreach

Posted 1 month ago by hjortur17

Hi, I have a Booking and Service table and a pivot table to connect them. I'm trying to get the services for these bookings but it always renders the else statement:

@if (!$booking->services->isEmpty())
        <div class="flex my-4">
            <div class="w-64 self-center">
                <h3 class="font-light text-xl">Þjónusta:</h3>
            </div>
            <div class="flex-1 text-right">
                <ul>
                    @foreach ($booking->services as $service)
                        <li>
                            <p class="font-bold">{{ $service->description }}</p>
                        </li>
                    @endforeach
                </ul>
            </div>
        </div>
    @else
        <div class="flex my-4">
            <h3 class="font-light text-xl italic">Ekki var valið þjónusta</h3>
        </div>
    @endif

I have a booking inside my database (id: 14) and then I have a row in booking_service (14, 3) and then I have a service in my database (id: 3). And I'm trying to get the description of the service.

It was working before, then I changed from this:

$bookings = Booking::where('dropOffDate', $today)->orderBy('flightTime', 'asc')->get();

to this:

$bookings = Booking::where('dropOffDate', $today) ->orWhere('pickUpDate', $today) ->orderBy('flightTime', 'asc') ->get();

Please sign in or create an account to participate in this conversation.