2016-11-29 62 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;