PostgreSQL错误和Laravel一起

时间:2017-05-30 11:32:02

标签: postgresql laravel

我正在尝试提出请求,当我在LIKE字段上使用created_at时出现错误。
我使用Laravel 5.3和Eloquent(ORM)。

$date = Carbon::now();
foreach ($hours as $hour) {
     $chart[$hour]['hour'] = $hour;
     $chart[$hour]['allowed'] = VisitsAllowed::where('created_at', 'LIKE', Carbon::parse($date)->format('Y-m-d %'))->count();
     $chart[$hour]['denied'] = VisitsDenied::where('created_at', 'LIKE', Carbon::parse($date)->format('Y-m-d %'))->count();
}

错误:

SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: timestamp without time zone ~~* unknown
LINE 1: ... aggregate from "visits_allowed" where "created_at" LIKE $1
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. (SQL: select count(*) as aggregate from "visits_allowed" where "created_at" LIKE 2017-05-30 %)

有人可以帮我找到解决方案。

2 个答案:

答案 0 :(得分:0)

请改为尝试:

foreach ($hours as $hour) {
     $chart[$hour]['hour'] = $hour;
     $chart[$hour]['allowed'] = VisitsAllowed::where('created_at', '=', Carbon::parse($date)->format('Y-m-d'))->count();
     $chart[$hour]['denied'] = VisitsDenied::where('created_at', '=', Carbon::parse($date)->format('Y-m-d'))->count();
}

答案 1 :(得分:0)

不是最漂亮的解决方案,但测试日期(比如2017-05-30)是否介于:yyyy-mm-dd 00:00:00和yyyy-mm-dd 23:59:59之间。

$chart[$hour]['allowed'] = VisitsAllowed::where('created_at',  '>=', Carbon::parse($date )->format('Y-m-d') . ' 00:00:00')
            ->where('created_at',  '<=', Carbon::parse($date )->format('Y-m-d') . ' 23:59:59')
            ->count();

$chart[$hour]['denied'] = VisitsDenied::where('created_at',  '>=', Carbon::parse($date )->format('Y-m-d') . ' 00:00:00')
            ->where('created_at',  '<=', Carbon::parse($date )->format('Y-m-d') . ' 23:59:59')
            ->count();