2017-04-12 56 views
0

我打電話產品的信息字段,你可以看到如下:處理失敗的雄辯方法

$product->attributes->first()->attributeValues->where('locale',$iso); 

基本上在$product變量已經有關於產品的信息。

我使用$product->attributes->first()來獲得他的屬性,並在得到它們後,我用->attributeValues->where('locale',$iso)來獲得他的值與特定的語言。

它輸出的數據是好的,但只有屬性存在,因爲如果沒有任何它沒有,並且由於attributeValues方法頁面失敗。

我該如何處理這種情況?

+0

你在哪裏打電話呢?在模型或控制器? – Laerte

回答

0

如果您願意,您可以用簡單的empty()甚至count()進行檢查。

$attributes = $product->attributes->first()->attributeValues->where('locale',$iso); 

if (count($attributes) == 0) { 
    // There is no attribute, do something 
} 
0

分手了你的行

$attributes = $product->attributes->first(); // placeholder 

if(isset($attributes) { // check if we have one 
    $attributes->attributeValues->where('locale',$iso); // if so.. do the dance 
} else {// go home }