samedi 23 mars 2019

Eloquent receive a value like a column name?

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 en 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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire