Eugene Anthony
Eugene Anthony

Reputation: 49

Laravel orderBy object attribute

The 'price' is an object that has an attribute known as list_price.

$products =
Product::with(['child','price','variant','thumbnail','image',
'description'])->
take($recperpage)->get();

I intend to order by list_price attribute. I can't specify the query as follow:

->orderBy('price.list_price','desc')

It doesn't work. Any help? I'm using Laravel 5.3.

Upvotes: 0

Views: 55

Answers (1)

Jonas Staudenmeir
Jonas Staudenmeir

Reputation: 25906

If you want to order the query, you have to add a JOIN on the prices tables.

The easier solution is ordering the query result:

$products = Product::with(['child','price','variant','thumbnail','image', 'description'])
    ->take($recperpage)->get()->sortByDesc('price.list_price');

Upvotes: 2

Related Questions