Getting just the latest value on a joined table with Eloquent

后端 未结 2 1480
自闭症患者
自闭症患者 2020-11-29 07:31

I have two tables like this:

products:

+----+-----------+
| id |   name    |
+----+-----------+
|  1 | Product 1 |
|  2 | Product 2          


        
2条回答
  •  旧巷少年郎
    2020-11-29 08:13

    You can tweak your relations to get what you want. Accepted answer of course works, however it might be memory overkill with lots of data.

    Find out more here and there.

    Here's how to use Eloquent for this:

    // Product model
    public function latestPrice()
    {
       return $this->hasOne('Price')->latest();
    }
    
    // now we're fetching only single row, thus create single object, per product:
    $products = Product::with('latestPrice')->get();
    $products->first()->latestPrice; // Price model
    

    That's nice, but there's more. Imagine you'd like to load highest price (just a value) for all the products:

    public function highestPrice()
    {
       return $this->hasOne('Price')
          ->selectRaw('product_id, max(price) as aggregate')
          ->groupBy('product_id');
    }
    

    Not very convenient yet:

    $products = Product::with('highestPrice')->get();
    $products->first()->highestPrice; // Price model, but only with 2 properties
    $products->first()->highestPrice->aggregate; // highest price we need
    

    So add this accessor to make the life easier:

    public function getHighestPriceAttribute()
    {
        if ( ! array_key_exists('highestPrice', $this->relations)) $this->load('highestPrice');
    
        $related = $this->getRelation('highestPrice');
    
        return ($related) ? $related->aggregate : null;
    }
    
    // now it's getting pretty simple
    $products->first()->highestPrice; // highest price value we need
    

提交回复
热议问题