Skip to content Skip to sidebar Skip to footer

Many-to-many Relationship Orderby - Laravel Query Builder

I am trying to order products by thp price in options table the price can be in multi currency for that i add dolor_price calculated as code below. but the result was the follow

Solution 1:

Try this:

$rateEruToDolor =2;
$rateAedToDolor =3;
$products = Product::join('product_options', 'products.id', '=', 'product_options.product_id')
      ->join('options', 'product_options.option_id', '=', 'options.id')
      ->selectRaw('price, currency, IF(currency=0, price * ?, IF(currency=1, price * ?, price)) 
                   as dolor_price', [$rateEruToDolor, $rateAedToDolor]);
$products->orderBy('dolor_price');
dd($products->get()->toArray());

List all of the fields in your products/options table along with price and currency already listed in the selectRaw('xxx, xxx, price, currency, IF ...) method above. Hope this helps

Post a Comment for "Many-to-many Relationship Orderby - Laravel Query Builder"