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

前端 未结 5 1666
慢半拍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:41

    Use Eloquent accessors

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

    Then

    $product->lowest;
    

提交回复
热议问题