如何通过仅使用y-m-d零件来从创建的日期中获取收集项目



我有一个包含记录的集合,其中包括创建的row。现在,我想在没有时间的情况下按日期获得特定的行。我的意思是:

$users->whereLoose('created_at', '2016-11-23')

当然,这是空的,但是我需要使用的格式是" y-m-d",而没有" h:i:s"部分。如何使用此格式从集合中获取记录?

您也可以使用filter方法和isSameDay()方法。

$date = CarbonCarbon::parse('2016-11-23');
$usersForDate = $users->filter(function ($user) use ($date) {
    return $user->created_at->isSameDay($date);
});

希望这会有所帮助!

您可以做到这一点:

$date = Carbon::parse('2016-11-23');
$users->where('created_at', '>', $date->startOdDay())
      ->where('created_at', '<', $date->endOfDay());

,也可以尝试以下方法:

$users->whereDate('created_at', '=', '2016-11-23');

您可以尝试使用文档的集合上的映射()函数https://laravel.com/docs/5.2/collections:

https://laravel.com/docs/5.2/collections#method-map

$collection = collect(['taylor', 'abigail', null])->map(function ($name) {
    return strtoupper($name);
})
->reject(function ($name) {
    return empty($name);
});

最新更新