What you are trying to achieve can be done via accessor and gladly, you have already gone through half of the steps. Just change the name of your method in model as per Laravel's accessor naming convention like this:
public function ProductSeller()
{
return $this->hasMany('AppProductSeller','product_id');
}
public function GetPriceAttribute()
{
$productSellers = $this->ProductSeller;
foreach ($productSellers as $productSeller){
$productPrice[]=$productSeller->price;
}
if (empty($productPrice) == true || min($productPrice) == '0'){
$Rprice = 'call for price';
} else {
$Rprice = number_format(min($productPrice));
}
return $Rprice;
}
Now, you can easily access the method like:
$product->price;
Note: In the model, get() function is removed from the end of the relationship and also, the relationship method is called without adding parenthesis in the GetPriceAttribute() method. This approach worked for me.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…