2017-04-12 61 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 }