Reputation: 856
I have the follow model in Laravel Eloquent
:
<?php
namespace Sac\Models;
use Illuminate\Database\Eloquent\Model as Eloquent;
class PatientsDevices extends Eloquent
{
...
public static function getDevicesByDateTime()
{
$currrentDateTime = (new \DateTime('now', new \DateTimeZone('America/Costa_Rica')))->format('Y-m-d H:i:s');
return self::where('monitor', '=', 1)
->whereBetween($currrentDateTime, ['start_date_time', 'end_date_time'])
->get();
}
...
}
With this, I try to build the next SQL sentence:
SELECT *
FROM `patients_devices`
WHERE
(
`monitor` = 1 AND
'2019-03-23 13:11:48' BETWEEN `start_date_time` AND `end_date_time`
)
But instead Eloquent build:
SELECT *
FROM `patients_devices`
WHERE
(
`monitor` = 1 AND
`2019-03-23 13:11:48` BETWEEN `start_date_time` AND `end_date_time`
)
The little big difference are (') and (`) (backquote/backtick) into where condition, since the first is recognized as string but the second like a literal column name.
In my model, I have a public method that get retrive to data collection with theses conditions: the monitor value is 1 and a timestamp exists between two datetime columns (start and end).
My problem: I'm forced in use models, and I see that I use of whereBetween
method, recognize $currrentDateTime
like as column when should recognize as value, since in SQL I can use columns and values positions on the clause where whitout restrincts.
This is a Eloquent
limitation? or I'm developing of wrong way the logic SQL?. I can resolve it the other way using models?
Upvotes: 0
Views: 452
Reputation: 12847
Don't you want to get the entries in between these dates?
self::where('monitor', '=', 1)
->where('start_date_time', '<=', $currentDateTime)
->where('end_date_time', '>=', $currentDateTime)
->get();
If you want to use whereBetween()
, the syntax is ->whereBetween('column', [values])
, so I doubt it fits in your case.
Upvotes: 2