G
G
GaserV2017-08-12 10:50:35
Laravel
GaserV, 2017-08-12 10:50:35

How to group and sort grouped objects?

Such a task. It is necessary to group products in such a way that those with status = 3 || 4, and after all the rest. And first, products with status = 4 are displayed, then with status = 3, and then all the rest, regardless of this field. Those. in theory, you need to make 3 groups: 1) With status 4. 2) With status 3. 3) all the rest sorted by created_at and regardless of the status field. How to implement this? I'm attaching the code below, but it's just grouping and in ASC order.

$orders = Order::all()->groupBy('status');
      $orders->toArray();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GaserV, 2017-08-13
@GaserV

I made a crutch, it works) But maybe someone has a better option?

$orders = [];

      $usOrders  = Project::where('status', '>', '2')->orderBy('status', 'desc')->get();
      array_push($orders , $usOrders);

      $allOrders = Project::where('status', '<', '3')->orderBy('created_at', 'desc')->get();
      array_push($orders , $allOrderss);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question