2017-05-30 107 views
0

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

$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 %) 

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

+0

你可以举一个例子为$ date吗? –

+0

也许尝试''ILIKE''而不是''LIKE'' –

+0

ILIKE不工作。 – Pixel

回答

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(); 
} 
+0

与ILIKE出现同样的错误 – Pixel

+0

即使从格式中删除'%'并将其添加到旁边吗? –

+0

没有% – Pixel

0

不是最漂亮的解决方案,但如果测试的日期(比如二零一七年五月三十零日)之间: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();