使用accepted answer here,我想根據created_at
返回所有對象。如何在日期爲字符串時查詢日期表
$projects = [ {id => 1,..., 'created_at' => "2017-05-15 14:46:32",...}, ]
$time = strtotime('2017-05-15'); // format issue?
$newformat = date('Y-m-d',$time); // "2017-05-15"
$projects = $user->projects->where('created_at', $newformat);
即時得到回$projects
空數組,在tinker
相同。是的,雄辯是完美的。我以正確的方式去做嗎?
既然我在這裏,我需要查詢兩個日期,即$from
和$to
與whereBetween
:
$projects = $user->projects->whereBetween('created_at', [$from, $to])->toArray();
我不記得在那裏我看到從whereBetween
但給我的錯誤: $from
和$to
會類似於$newformat
。
Method whereBetween does not exist.
'$ projects = $ user-> projects-> whereBetween('created_at',[$ from,$ to]) - > get();'你試過這個嗎? –
https://laravel.com/docs/5.2/queries#where-clauses –
@QuỳnhNguyễn我,是的,我已經嘗試過,但不斷收到'方法whereBetween不存在。我需要將它添加到我的模型嗎?對不起,我是laravel的新手。我有一個閱讀。謝謝 – Sylar