2

我正在尝试通过将日期的到期日期与今天的日期进行比较来过滤一组对象,但我无法让它正常工作。任何人都可以看看代码。我确定我在这里遗漏了一些东西。我到处搜索,发现了很多例子,但没有一个有效。

这符合得很好,但包含 9 个对象的列表,其中 3 个对象的到期日期设置为 2012,它不会从集合中返回任何结果。

控制器

public function classifieds()
{   
    $myclassifieds = Auth::user()->classifieds;
    return view('account.classifieds')->with(['allclassifieds'=>$myclassifieds]);
}

看法

@if(count($allclassifieds->where('expired_on','<', Carbon\Carbon::now() ) ) > 0)
//Something here
@endif
4

1 回答 1

1

expired_on直接访问值

@foreach($allclassifieds as $classified)
    @if($classified->expired_on > Carbon\Carbon::now())
        // show classified
    @endif
@endforeach

默认情况下,您可以通过将 expired_on 属性添加到$dates模型中的数组来使该属性成为 Carbon 实例:

protected $dates = ['created_at', 'updated_at', 'expired_on'];

现在expired_on总是返回一个 Carbon 实例

于 2016-04-05T19:01:10.530 回答