6

Trying to order my table by newest Date first, then the status. The Status can be (New, In Review, Declined, Closed) and Status is an enum field in the database.

I've tried several things with no success. This is the closest I've got with this code. I'm new to Laravel so not sure how to do this.

$inquiries = Inquiry::all()->sortByDesc('created_at')->sortByDesc('Status')->values();

That produces this:

--------------------------------------------------------------
|     Name          |      Created At    |      Status       |
--------------------------------------------------------------
|     Richard       |      05/02/2020    |       New         |
--------------------------------------------------------------
|     James         |      05/02/2020    |       New         |
--------------------------------------------------------------
|     Christine     |      04/16/2020    |      Declined     |
--------------------------------------------------------------
|     Charles       |      04/14/2020    |       New         |
--------------------------------------------------------------
|     Judy          |      03/13/2020    |      In Review    |
--------------------------------------------------------------
|     Paul          |      01/12/2020    |       New         |
--------------------------------------------------------------

I would like it to look like this where it's sorted by Date, with the newest Date first, then with status = "New". I would like it to look like this:

--------------------------------------------------------------
|     Name          |      Created At    |      Status       |
--------------------------------------------------------------
|     Richard       |      05/02/2020    |       New         |
--------------------------------------------------------------
|     James         |      05/02/2020    |       New         |
--------------------------------------------------------------
|     Charles       |      04/14/2020    |       New         |
--------------------------------------------------------------
|     Paul          |      01/12/2020    |       New         |
--------------------------------------------------------------
|     Christine     |      04/16/2020    |      Declined     |
--------------------------------------------------------------
|     Judy          |      03/13/2020    |      In Review    |
--------------------------------------------------------------
Fly_Moe
  • 239
  • 1
  • 2
  • 11
  • Is the date in that format on your database? Why you are sorting by 'created_at' but showing 'Date' on that tables? – porloscerros Ψ May 02 '20 at 16:45
  • so you want to sort by status first (more priority) then sort by date ASC (use sortByAsc instead of sortByDesc) – N69S May 02 '20 at 16:45
  • `$inquiries = Inquiry::orderBy('created_at','desc')->orderBy('Status','desc')->get();` – Christophe Hubert May 02 '20 at 16:49
  • @porloscerrosΨ Correct, date should be 'Create At' not "Date". I edited it. – Fly_Moe May 02 '20 at 17:32
  • Ok, I was going to suggest almost the same as in Christophe Hubert's comment, in other words, ordering in the database query, but orderning by staus first and then by date `$inquiries = Inquiry::orderBy('Status','desc')->orderBy('created_at','desc')->get();` – porloscerros Ψ May 02 '20 at 17:38
  • This worked for me. Thanks for the help guys. ```$inquiries = Inquiry::orderBy('status','asc')->orderBy('created_at','desc')->get();``` – Fly_Moe May 02 '20 at 18:13

2 Answers2

11

First let's see whether you need to use orderBy() or sortByDesc().

sortBy() / sortByDesc()

The methods sortBy() and sortByDesc() use to sort the data after fetched results from the database.

orderBy()

The method orderBy is same as the SQL ORDER BY statement. This will sort the database records then output the results.


Answer

Here you need to use orderBy() instead of sortByDesc(). It's almost a duplicate question, Find the original here.

// Make sure column names are correct
$inquiries = Inquiry::orderBy('status', 'ASC') // or DESC
    ->orderBy('created_at', 'DESC')
    ->get();

BadPiggie
  • 5,471
  • 1
  • 14
  • 28
0

You can pass a function to the sortBy function on the collection allowing you to specify which value should be used for sorting. This way, you can also combine two values for sorting.

Inquiry::all()->sortBy(function ($inquiry) {
    return sprintf('%s%s', $inquiry->created_at, $inquiry->status);      
})->values();

You can read more about it here: https://laravel.com/docs/collections#method-sortby

vblinden
  • 390
  • 3
  • 17