在laravel 5中基于created_at时间戳检索模型

时间:2016-09-04 10:56:55

标签: php laravel laravel-5 eloquent

$today = Carbon::today();
$search = $today->subDay(1);
$loading =LoadingBus::where('created_at','=',$search)->get();
dd($loading);

给了我一个空集合。请问我做错了什么     收集{#279▼     #it​​ems:[]     } 我的loading_buses表中的数据与此特定日期匹配 了解更多信息

dd($search)
Carbon {#278 ▼
+"date": "2016-09-03 00:00:00.000000"
+"timezone_type": 3
+"timezone": "UTC"
}
loading_buses created_at value = 2016-09-03 00:01:06

我真的需要帮助

2 个答案:

答案 0 :(得分:0)

您不应传递 Carbon 对象,而应传递代表日期的字符串。

替换

$loading = LoadingBus::where('created_at','=',$search)->get();

$loading = LoadingBus::where('created_at','=',$search->formatLocalized('%Y%m%d%H%M%S'))->get();

答案 1 :(得分:0)

您的Carbon实例与小时,分钟和秒的数据库记录不相等,您应该唱whereDate mehtod。 试试这个:

$today = Carbon::today();
$search = $today->subDay(1);
$loading =LoadingBus::whereDate('created_at','=',$search->format('Y-m-d'))->get();
dd($loading);