2017-05-30 48 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();