I'm trying to understand how Eloquent select changes the results of a query. For example:
$startDate = Carbon::createFromFormat('Y-m-d H:i:s', '2023-01-26 00:00:00', 'America/Chicago')->timezone('UTC');
$endDate = Carbon::createFromFormat('Y-m-d H:i:s', '2023-01-26 23:59:59', 'America/Chicago')->timezone('UTC');
$data = Orders::where('canceled', 0)
->whereBetween('created_at', [$startDate->toDateTimeString(), $endDate->toDateTimeString()])
->where(function ($query) {
$query->where('is_branding', 1)
->orWhere('is_premium_branding', 1);
})
->get();
I have some other code running a foreach on this data to calculate and I end up with:
{
"branding_order_items_count": 12,
"branding_order_items_sales": 799.98,
"branding_order_items_margin": 169.71,
"branding_order_items_margin_percent": 0
}
However, if I run the same query but with an added select and calculate through the same foreach loop, I get a different result:
$startDate = Carbon::createFromFormat('Y-m-d H:i:s', '2023-01-26 00:00:00', 'America/Chicago')->timezone('UTC');
$endDate = Carbon::createFromFormat('Y-m-d H:i:s', '2023-01-26 23:59:59', 'America/Chicago')->timezone('UTC');
$data = Orders::where('canceled', 0)
->whereBetween('created_at', [$startDate->toDateTimeString(), $endDate->toDateTimeString()])
->where(function ($query) {
$query->where('is_branding', 1)
->orWhere('is_premium_branding', 1);
})
->select('*', DB::raw("count(*) as count")) // <<<<<<< Added this
->get();
With that added select, I get the following:
{
"branding_order_items_count": 11,
"branding_order_items_sales": 649.99,
"branding_order_items_margin": 142.12,
"branding_order_items_margin_percent": 0
}
The first result is the correct numbers, 12 items / 799.98, etc. So why does adding the select to the eloquent query return a different result, and how can I have it return the correct results while still using the select?
TIA
->get(), what you can do is using$count = count($data);$startDate = Carbon::parse('2023-01-26 00:00:00', 'America/Chicago')->tz('UTC')and inside eloquent query ->whereBetween also accept Carbon, no need to convert it to DateTimeString. ... by the way be careful when search between 00:00:00 to 23:59:59, you might skip transaction happen exactly @ 23:59:59, best to do is$current_tz = 'America/Chicago'; $server_tz = 'UTC'; $date = Carbon::parse('2023-01-26', $current_tz); $startDate = $date->tz($server_tz); $endDate = $date->addDay()->tz($server_tz);$data->count()will also work