我有一个产品型号Laravel如何在Eloquent模型中添加自定义函数?
class Product extends Model
{
...
public function prices()
{
return $this->hasMany('App\Price');
}
...
}
我想补充这将返回的最低价格的功能,并在控制器我可以用获取的价值:我加入这产品型号
Product::find(1)->lowest;
:
public function lowest()
{
return $this->prices->min('price');
}
但我得到了一个错误说:
Relationship method must return an object of type Illuminate\Database\Eloquent\Relations\Relation
如果我使用Product::find(1)->lowest();
,它会工作。是否有可能获得Product::find(1)->lowest;
的工作?
任何帮助,将不胜感激。
没有OFC因为你调用一个方法,我认为TGAT只会工作一个hasone关系,有什么区别?有还是没有! –