Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

PoundXI's avatar

How to orderBy aliased column of relations model using Eloquent

How to get all warehouses matched where conditions and ordered by warehouse that has lowest price product.

This is my tables.

warehouses => [id]

products => [ id, warehouse_id, price ]

This is what I try to do. I dont known what correct syntax should be.

Warehouses::with('products')->where(...)->whereHas(...)
	->select(['*', DB::raw('GROUP BY products.id .... MIN(products.price) as warehouse_min_product_price')])
	->orderBy('warehouse_min_product_price', 'asc');

Thanks.

0 likes
0 replies

Please or to participate in this conversation.