Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过比较日期过滤视图中的laravel集合?_Php_Laravel_Eloquent_Laravel 5.1_Laravel Collection - Fatal编程技术网

Php 通过比较日期过滤视图中的laravel集合?

Php 通过比较日期过滤视图中的laravel集合?,php,laravel,eloquent,laravel-5.1,laravel-collection,Php,Laravel,Eloquent,Laravel 5.1,Laravel Collection,我试图通过比较过期日期和今天的日期来过滤对象集合,但我无法完全让它工作。有人能看一下代码吗。我肯定我错过了什么。我到处寻找,找到了很多例子,但没有一个是有效的 这很好地符合了9个对象的列表,其中3个对象的到期日期设置为2012年,但它不会从集合中返回任何结果 控制器 public function classifieds() { $myclassifieds = Auth::user()->classifieds; return view('account.class

我试图通过比较过期日期和今天的日期来过滤对象集合,但我无法完全让它工作。有人能看一下代码吗。我肯定我错过了什么。我到处寻找,找到了很多例子,但没有一个是有效的

这很好地符合了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

@if(count($allclassifieds->where('expired_on'),直接访问值
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\u on
总是返回一个Carbon实例

看起来不错,很难说没有看到集合本身。你确定
expired\u on
可以直接在每个集合对象的第一级上使用吗?另外,
where()
比较严格。可能是您的
expired_on
是一个日期,而Carbon::now()为您提供了datetime-这可能会导致问题,不确定。这是一个包含一个对象的集合。Expires_on是一个日期时间对象,它非常奇怪。[{“id”:31,“username”:“test”,“type”:1,“action”:“sell”,“maincat”:“Antiques”,“title”:“test”,“description”:“test”,“extextextfield”:null,“lowerprice”:1,“partex”:0,“condition”:“used”,“price”:“680”,“images”:“1”,“email”:test@test.com,“评论”:1,“创建时间”:“2016-04-03 19:00:26”,“更新时间”:“2016-04-03 19:00:26”,“expires_on”:“2016-03-14 19:00:26”}]在你的代码中,你已经
过期了
。这是在帖子中输入的一个拼写错误还是在原始代码中的一个拼写错误?;)对象中包含
expires_on
。这是我的错,它在视图中实际上写得很好这确实有效,但我必须解析()$classified->expired\u成功比较。这不完全是我想要的,但这是可行的way@matpulis更新的答案。你能详细说明你在找什么吗。我想计算一下集合中有多少项的过期日期比今天小。实际上不必执行for循环。尽管你的方法可以工作,谢谢you@matpulis哦,我明白了。我会让控制器处理它。然后将它作为变量发送到视图。我就是这么做的。奇怪的是->where('expired_on','