In laravel 10 app need to path 2 dates parameters into whereRaw and I do it as :
\Log::info(varDump($filterAtDate, ' -19 retrieveDbData $filterAtDate::'));
$filterAtDateTill = (clone $filterAtDate->addDays(10));
\Log::info(varDump($filterAtDateTill, ' -1 $filterAtDateTill::'));
$this->reactions = Reaction
::query()->whereRaw('created_at BETWEEN ? AND ?', [ $filterAtDate, $filterAtDateTill ])
->groupBy('action')
->orderBy('reaction_count', 'desc')
->select(
$reactionTb . '.action',
DB::raw('count(' . $reactionTb . '.id) as reaction_count'))
->get()->toArray();
}
Checking logs for these 2 vars I see valid dates :
[2024-05-24 09:35:22] local.INFO: (Object of Carbon\Carbon) : -19 retrieveDbData $filterAtDate:: : Array
(
[ * endOfTime] =>
[ * startOfTime] =>
[ * constructedObjectId] => 000000000000061a0000000000000000
[ * localMonthsOverflow] =>
[ * localYearsOverflow] =>
[ * localStrictModeEnabled] =>
[ * localHumanDiffOptions] =>
[ * localToStringFormat] =>
[ * localSerializer] =>
[ * localMacros] =>
[ * localGenericMacros] =>
[ * localFormatFunction] =>
[ * localTranslator] =>
[ * dumpProperties] => Array
(
[0] => date
[1] => timezone_type
[2] => timezone
)
[ * dumpLocale] =>
[ * dumpDateProperties] =>
[date] => 2024-05-20 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Kiev
)
[2024-05-24 09:35:22] local.INFO: (Object of Carbon\Carbon) : -1 $filterAtDateTill:: : Array
(
[ * endOfTime] =>
[ * startOfTime] =>
[ * constructedObjectId] => 00000000000006220000000000000000
[ * localMonthsOverflow] =>
[ * localYearsOverflow] =>
[ * localStrictModeEnabled] =>
[ * localHumanDiffOptions] =>
[ * localToStringFormat] =>
[ * localSerializer] =>
[ * localMacros] =>
[ * localGenericMacros] =>
[ * localFormatFunction] =>
[ * localTranslator] =>
[ * dumpProperties] => Array
(
[0] => date
[1] => timezone_type
[2] => timezone
)
[ * dumpLocale] =>
[ * dumpDateProperties] =>
[date] => 2024-05-30 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Kiev
)
But when I trace sql I see invalid sql, as 2 parameters are equal :
SELECT `reactions`.`action`, count(reactions.id) AS reaction_count
FROM `reactions`
WHERE created_at BETWEEN '2024-05-30 00:00:00' AND '2024-05-30 00:00:00'
GROUP BY `action`
ORDER BY `reaction_count` desc
So 1st parameter was not passed and I used clone method to create a second var
Also I tried to use named parameters with whereRaw :
::query()->whereRaw('created_at BETWEEN :dateFrom AND :dateTill ', [ 'dateFrom' => $filterAtDate, 'dateTill' => $filterAtDateTill ])
But in this case tracing sql I see:
SELECT `reactions`.`action`, count(reactions.id) AS reaction_count
FROM `reactions`
WHERE created_at BETWEEN :dateFrom AND :dateTill
GROUP BY `action`
ORDER BY `reaction_count` desc
How to make it correctly ?