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

ya-ya-it's avatar

Query builder not working

Hello!

I have a difficult time building the query for the table.

There is what I need to do:

dd(Bookings::where('status', '!=', 'in-progress')->where('game_id', 2)->where('method', 'online')->count());

I need to split that query for a few pieces.

$totalBookings = Bookings::query()->where('status', '!=', 'in-progress');
...
$totalBookings = $totalBookings->where('game_id', 2)->where('method', 'online');
...
$summaryGame = new \stdClass();
$summaryGame->all = $totalBookings->count();
$summaryGame->checked = $totalBookings->where('status', 'checked-in')->count();
$summaryGame->notShown = $totalBookings->where('status', 'not-shown')->count();
$summary->push($summaryGame);

But I have the result only for $summaryGame->all, everything else gives me 0.

Could you, please, help me, if I misunderstood the concept of the query building? Thanks!

0 likes
8 replies
yibr's avatar

maybe you don't have records that match your query

try dd() for each query separate

a4ashraf's avatar

@ya-ya-it

Try this

$bookings = DB::table('bookings')->where([
    ['status', '!=', 'in-progress'],
    ['game_id', 2],
    ['method', 'online'],
])->count();

ya-ya-it's avatar

I do have records, but for some reason, they are not displaying.

ya-ya-it's avatar

The regular

Bookings::where('status', '!=', 'in-progress')->where('game_id', 2)->where('method', 'online')->count());

works, as well as your query. But I need to split it into a few queries to make where dynamic and at this point it breaks.

a4ashraf's avatar

@ya-ya-it

remove query() from your statement


$bookings = Bookings::where('status', '!=', 'in-progress')->where('game_id', 2)->where('method', 'online')->get();

    $totalBookings= $totalNotShown = $totalCheckedIn = 0;

    foreach($bookings as $booking) {

        if($status == 'checked-in')
            $totalCheckedIn++;
        elseif($status == 'not-shown')
            $totalNotShown++;
        else
            $totalBookings++;
    }


    $summaryGame = new \stdClass();
    $summaryGame->all = $totalBookings;
    $summaryGame->checked = $totalCheckedIn;
    $summaryGame->notShown = $totalNotShown;
    $summary->push($summaryGame);
a4ashraf's avatar
a4ashraf
Best Answer
Level 33

@ya-ya-it

Try now


$bookings = Bookings::where('status', '!=', 'in-progress')->where('game_id', 2)->where('method', 'online')->get();

    $totalBookings= $totalNotShown = $totalCheckedIn = 0;

    foreach($bookings as $booking) {

        if($status == 'checked-in')
            $totalCheckedIn++;
        elseif($status == 'not-shown')
            $totalNotShown++;
        else
            $totalBookings++;
    }


    $summaryGame = new \stdClass();
    $summaryGame->all = $totalBookings;
    $summaryGame->checked = $totalCheckedIn;
    $summaryGame->notShown = $totalNotShown;
    $summary->push($summaryGame);
ya-ya-it's avatar

Work like a charm! Thanks! Hm, I'm just wondering now, why the query builder is appending parameters every time instead of overriding it?

Please or to participate in this conversation.