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),
]
);