0

I'm working on an e-commerce project and on the client side we have two 'sort by option' Cheapest and Expensive and for order I'm using the code below and it's ok!


$query->select([
     '*',
     DB::raw(
     'IF(`discountPrice` IS NOT NULL, `discountPrice`, `price`) `sortPrice`',
     ),
     ])->orderBy('sortPrice', 'ASC')->paginate(15);

but I don't want the products with zero quantity to show up on the first if their price is high or low. they should be constantly on the bottom of the pagination if their quantity is 0;

any solutions?

1
  • Can you share some data, and table definition Commented Feb 2, 2023 at 9:06

1 Answer 1

1

To push all the 0 quantity products to the bottom of your result set you need to turn your quantity into a 1 or 0 (in stock / out of stock) for ordering as you do not want to order by quantity descending, as that would negate the later sorting (ASC or DESC) by price -

$query->select([
    '*',
    DB::raw(
        'IF(`discountPrice` IS NOT NULL, `discountPrice`, `price`) `sortPrice`',
    ),
    ])->orderByRaw('IF(quantity = 0, 0, 1) DESC')
      ->orderBy('sortPrice', 'ASC')
      ->paginate(15);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.