将此查询转换为eloquent

时间:2014-08-07 22:58:59

标签: mysql laravel eloquent str-to-date

我是Laravel的菜鸟。任何人都可以帮助我在雄辩的

中写这个查询
SELECT
    *
FROM
    table
WHERE
    (
        STR_TO_DATE(`date`, '%m/%d/%Y') BETWEEN '2014-08-05'
        AND '2014-08-05'
    )

ORDER BY
    id

3 个答案:

答案 0 :(得分:3)

假设您的模型名为Table,假设您的字段属于DATE类型,则可以执行以下操作:

Table::where('date', '>=', '2014-08-05')
    ->where('date', '=<', '2014-08-05')
    ->get();

或者你可以这样做:

Table::select('table.*', DB::raw("STR_TO_DATE(date, '%m/%d/%Y') as date_format"))
    ->where('date_format', '>=', '2014-08-05')
    ->where('date_format', '=<', '2014-08-05')
    ->get();

答案 1 :(得分:2)

如果您想按原样使用查询,只需使用DB :: raw

即可

http://laravel.com/docs/queries#raw-expressions

DB::raw(SELECT * FROM table WHERE ( STR_TO_DATE(date, '%m/%d/%Y') BETWEEN '2014-08-05' AND '2014-08-05' ) ORDER BY id);

答案 2 :(得分:0)

你可以试试这个:

$from = '...';
$to = '...';

DB::table('table')->whereBetween('date', array($from, $to))->get();

或使用Eloquent:

ModelName::whereBetween('date', array($from, $to))->get();