Laravel how to add a custom function in an Eloquent model?

前端 未结 5 1683
慢半拍i
慢半拍i 2020-12-25 15:19

I have a Product model

class Product extends Model
{
    ...

    public function prices()
    {
        return $this->hasMany(\'App\\Price\');
    }

            


        
5条回答
  •  余生分开走
    2020-12-25 15:47

    change follow code

    public function lowest()
    {
        return $this->prices->min('price');
    }
    

    to

    // add get as prefix and add posfix Attribute and make camel case function
    public function getLowestAttribute()
    {
        return $this->prices->min('price');
    }
    

提交回复
热议问题