Laravel收藏日期比较 [英] Laravel Collection Date comparison

查看:103
本文介绍了Laravel收藏日期比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我一直在寻找答案一个小时,但似乎找不到答案. 我有一组不同日期的订单".我得到了本周的所有订单,这应该不成问题,然后我要查看一周中每一天的标签.

Alright so I have been looking for hours for an answer but can't seem to find one. I have an array of "orders" all for different dates. I get all of the orders for this week, this should not be an issue, then I want to a tab for each day of the week.

到目前为止,我尝试过的是:

What I have tried so far is this:

$dinnerOrders->where('date','>',$date)->where('date','<', $date->endOfDay())->sortBy('created_at');

$ date在哪里:

Where $date is :

$dt = Carbon\Carbon::create()->startOfWeek();
Carbon\Carbon::setTestNow($dt);
$date = new Carbon\Carbon('this ' . $day);

$ dinnerOrders通过以下方式获得:

And $dinnerOrders are get by:

$dinnerOrders = collect([]);
foreach($restaurant->dinners as $dinner) {
    foreach($dinner->orders as $order) {
        $dinnerOrders[$order->id] = $order;
    }
 }

日期似乎正确,并且用sql表示.因为$ dinnerOrders是一个集合,这行不通吗?

The dates seem correct and it words in sql. Is this not working because $dinnerOrders is a collection?

我得到的是什么,就是空集,尽管它可以在mysql终端中工作.

What I'm getting is nothing, i.e an empty set, despite it working in mysql terminal.

所以我要问的是,您可以对集合进行日期比较吗?如果可以,怎么办?

So what I'm asking is perhaps, can you do a date comparison on a collection? If so, how?

推荐答案

在您陈述问题时,$dinnerOrders是一个集合,而不是一个查询. Collection上的where()方法的工作原理略有不同.

As you state in your question, $dinnerOrders is a Collection, not a query. The where() method on the Collection works a little differently.

对于集合,where()方法不接受运算符.它仅进行相等比较.第一个参数是键,第二个参数是值,第三个参数是用于标记宽松比较(==)与严格比较(===)的布尔值.

For Collections, the where() method does not accept an operator. It only does equality comparisons. The first parameter is the key, the second is the value, and the third is a boolean to flag a loose comparison (==) vs a strict comparison (===).

您要寻找的是filter()方法.您传入一个闭包来进行日期比较.如果Closure返回true,则该项目保留在Collection中.如果返回false,则将该项目从Collection中移除.这样的事情(仅作为示例,可能需要调整逻辑):

What you're looking for is the filter() method. You pass in a Closure that does your date comparison. If the Closure returns true, the item stays in the Collection. If it returns false, the item is removed from the Collection. Something like this (just an example, the logic may need to be tweaked):

$dinnerOrders = $dinnerOrders->filter(function ($item) use ($date) {
    return (data_get($item, 'date') > $date) && (data_get($item, 'date') < $date->endOfDay());
});

帖子问题编辑

根据您编辑中提供的代码,我猜测一家餐厅有很多晚餐,而晚餐有很多订单.如果正确的话,您可以建立一个关系,说明一家餐厅在晚餐时有很多订单:

Post Question Edit

Based on the code provided in your edit, I'm guessing that a restaurant hasMany dinners, and a dinner hasMany orders. If this is correct, you can setup a relationship stating that a restaurant hasMany orders through dinners:

Restaurant.php:

Restaurant.php:

public function orders() {
    // restaurant has many orders through dinners
    $this->hasManyThrough('App\Order', 'App\Dinner');
}

通过这种关系设置,您可以使用查询生成器获取信息:

With this relationship setup, you could then get your information using the query builder:

$dinnerOrders = $restaurant->orders()->where('date','>',$date)->where('date','<', $date->endOfDay())->get();

这篇关于Laravel收藏日期比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆