我在Eloquent查询中遇到错误。
我的问题:

$mtd_hits = Usage::selectRaw("
        SUM(table_hits) AS hits,
    ")
    ->where('user_id', '=', $user->id)
    ->whereBetween('created_at', [$from_date, $to_date])
    ->first();

在我的课堂上:
public $timestamps = true;

protected $dates = ['created_at', 'updated_at'];

我的Usage$from_date$to_date实例:
Carbon
Carbon @1556323200 {#1416 ▼
  date: 2019-04-27 00:00:00.0 UTC (+00:00)
}

$from_date
Carbon @1558915200 {#1622 ▼
  date: 2019-05-27 00:00:00.0 UTC (+00:00)
}

按我的查询失败:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from `usage` where `user_id` = ? and `created_at` between ? and ? limit 1' at line 3
(SQL: select SUM(table_hits) AS hits, from `usage` where `user_id` = 2 and `created_at` between 2019-04-27 00:00:00 and 2019-05-27 00:00:00 limit 1)

如果这是个愚蠢的问题,请提前道歉。

最佳答案

一切都是关于你的原始查询!
尝试从代码中删除,
您还可以使用->toSql()方法查看查询

关于mysql - Laravel 5.8 Eloquent whereBetween失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55886101/

10-11 22:26
查看更多