0

I have an E-commerce website, I want to sort the products in product page by in-stock first so I did this and it works just fine:

$products = Product::orderByDesc('quantity')->get();

Now, I added a new column to the product table which is product_order that will take an integer so that I can custom sort the products, I tried the following code:

$products = Product::orderByDesc('quantity')->orderByDesc('product_order')->get();

The problem is that it only sorts them by quantity, the second orderByDesc is not effective, how can I make it effective?

Zack
  • 15
  • 3
  • What are the results, can you post an example of it not being effective? According to [Ordering](https://laravel.com/docs/8.x/queries#ordering) multiple `orderBy` are possible – brombeer Sep 04 '21 at 08:35
  • [How to Use Order By for Multiple Columns in Laravel 4?](https://stackoverflow.com/questions/17006309/how-to-use-order-by-for-multiple-columns-in-laravel-4) ... your current code should already be working. – Tim Biegeleisen Sep 04 '21 at 08:36

1 Answers1

0

I managed to solve the issue by doing the following:

$productsQty = Product::orderByDesc('product_order');
    $products = $productsQty->orderByDesc('quantity')->get();
    foreach ($products as $product) {
        if($product->quantity <= 0) {
            $product->product_order = 0;
            $product->save();
        }
    }
Zack
  • 15
  • 3