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

eggplantSword's avatar

Query unexpected behaviour

I'm using Inertia as well this particularly is a partial reload but I'm not sure that is the problem so I tagged the questions as Eloquent instead. This is the query in question, what happens is when I call it in one spot it'll say it counts 3 items but later when I try to get it I only get one result. I removed of lot of other code to just show this one query and every time it's manipulated.

$visited = SalePointCheckOut::query()
            ->whereDate('date', $request->date('filters.date') ?? now())
            ->when($request->input('filters.user_id') !== null, function ($query) use ($request) {
                $query->where('user_id', $request->input('filters.user_id'));
            })
            ->where('sale_point_entry', true)
            ->where('sale_point_exit', true);

$avg_time = $visited->get()->avg(function ($item) {
            return Carbon::parse($item->date_entry)->diffInMinutes(Carbon::parse($item->date_exit));
        });

return Inertia::render( 'Component', [
                  'chart_info' => [
                    'visited' => $visited->count(),
                    'last_visited' => $visited->count() > 0 ? $visited->orderByDesc('created_at')->first()->created_at->toDateTimeString() : now()->toDateTimeString()
                ],

                'chart_visited' => Inertia::lazy(fn() => $visited->get()),
            ]
        );

Why am I getting different results when trying to call chart_visited?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

It looks like you are calling the $visited query twice, once to get the $avg_time and once to get the chart_visited. The second time you call the query, you are not including the $avg_time calculation, so the query results may be different. To ensure that you are getting the same results each time, you should store the query results in a variable and use that variable for both calculations.

For example:

$visited = SalePointCheckOut::query()
            ->whereDate('date', $request->date('filters.date') ?? now())
            ->when($request->input('filters.user_id') !== null, function ($query) use ($request) {
                $query->where('user_id', $request->input('filters.user_id'));
            })
            ->where('sale_point_entry', true)
            ->where('sale_point_exit', true);

$visited_results = $visited->get();

$avg_time = $visited_results->avg(function ($item) {
            return Carbon::parse($item->date_entry)->diffInMinutes(Carbon::parse($item->date_exit));
        });

return Inertia::render( 'Component', [
                  'chart_info' => [
                    'visited' => $visited_results->count(),
                    'last_visited' => $visited_results->count() > 0 ? $visited_results->orderByDesc('created_at')->first()->created_at->toDateTimeString() : now()->toDateTimeString()
                ],

                'chart_visited' => Inertia::lazy(fn() => $visited_results),
            ]
        );

Please or to participate in this conversation.