vendredi 9 octobre 2020

Get Minimum value from Second Table in Laravel using relation

I have two tables

  1. Product List

id | name | user_id

1 Product 1 10 2 Product 2 15

  1. Price

id | list_id | price | type

1    1        250      1
2    2        350      2
3    1        500      1
4    2        600      2

Product listing table contain listing details and it is associated with the price table now i want to get the all the products and an extra field minimum price with every list. for eg. if i have product 1 listing then there i want to add extra field which extract minimum price 250 from price.

Listing::select('*')->whereHas('PriceVal', function($q) use($country) {
                        $q->where('Type','1');
                       })
                      ->selectRaw("count(*) as total")
                      ->where(['user_id' => $userId])->with('PriceVal')
                      ->get();

Relation:

 public function PriceVal()
    {
        return $this->belongsTo('App\Models\Price', 'id', 'list_id');
    } 


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire