2016-11-29 63 views
0

我想从集合中移除已过滤的集合,例如我有此返回的集合。筛选集合并删除集合雄辩laravel

$products = items::where('item_id',$request->item_id)->with('product_reviews')->with('product_reviews.profile')->get(); 
foreach($products->product_reviews as $r): 
    if($r->status==='unapproved'): 
     //remove this from 'products' collections because its not approved yet 
     $this->remove($this); 
    endif; 
endforeach; 

但这

$this->remove($this); 

不工作,既不是有效的语法删除图片集,我只是不知道如何例如去除过滤收集如果列状态包含“未批准”。任何想法,请帮助吗?

+1

为什么你不包括另一个考虑状态? – ggderas

+0

你可以在我的给定代码片段上发布示例基础吗? –

+0

我不知道该怎么做,因为我的目标是属于项目模型的产品。 –

回答

1

您可以在关系中添加条件。这未经测试,但很可能会解决您的问题。

items::where('item_id',$request->item_id)->with([ 
    'product_reviews' => function ($query) { 
     $query->where('status', 'approved')->with('profile'); 
    } 
])->get(); 
+0

它像魅力一样工作,谢谢! :) –

+0

完美!乐趣。 –

+0

啊我的坏,它不能工作不幸:(我的坏我在我的刀片有一个'如果'的声明,只有当状态被批准,为什么我认为它的工作,但它是由于我的刀片上的if语句 –

0

嗯..有点奇怪,因为其他2个解决方案应该适合您的情况。

您是否尝试过滤并将对象传递给其他数组并将它们显示在视图中?像:

$productWithReviews = items::where('item_id',$request->item_id)->with('product_reviews')->get(); 
$products = []; 
foreach($productWithReviews->product_reviews as $r): 
    if($r->status!=='unapproved'): 
     $products[] = $r; 
    endif; 
endforeach;