Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hjortur17's avatar

Pass two where statement into query

I'm trying to let Laravel get two columns out of the database, like this:

$bookings = Booking::where([
            ['dropOffDate', '=', '06/06/2019'],
            ['pickUpDate', '=', '06/06/2019']
        ])->orderBy('flightTime', 'asc')->toSql();

        dd($bookings);

But this returns:

"select * from `booking` where (`dropOffDate` = ? and `pickUpDate` = ?) order by `flightTime` asc"
0 likes
7 replies
Nakov's avatar

Can you please show an example of your expected output? Because what toSql() does is shows you the query that's being executed. You might want to use ->get() instead to get the results back.

hjortur17's avatar

So I was just using toSql() for debugging. But when I change it to get() I only get one booking, but I'm expect 4 bookings. (2x from dropOffDate and 2x from pickUpDate)

Nakov's avatar

@hjortur17 well then you need the date to either be dropOfDate OR pickUpDate so this is the query that you need:

Booking::where('dropOffDate', '06/06/2019')
    ->orWhere('pickUpDate', '06/06/2019')
    ->orderBy('flightTime', 'asc')
    ->get();
Cronix's avatar

(dropOffDate= ? andpickUpDate= ?) using AND there means BOTH conditions must be true. Using OR would mean EITHER can be true.

1 like
hjortur17's avatar

Okay but how can I render two separate divs? For example border green for dropOffDate and border red for pickUpDate?

@foreach ($bookings as $booking)
        @if ($booking->dropOffDate)
            <div class="border-t-8 border-green-400">
                <h1>{{ $booking->carNumber }}</h1>
            </div>
        @elseif ($booking->pickUpDate)
            <div class="border-t-8 border-red-400">
                <h1>{{ $booking->carNumber }}</h1>
            </div>
        @endif
    @endforeach
Snapey's avatar

Each record has both values or is one always null?

Does what you showed not work?

hjortur17's avatar

No, I am expanding 2 red boxes and 1 green but I get 3 boxes all with green.

Please or to participate in this conversation.