7

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    |
--------------------------------------------------------------
6
  • Is the date in that format on your database? Why you are sorting by 'created_at' but showing 'Date' on that tables? Commented May 2, 2020 at 16:45
  • so you want to sort by status first (more priority) then sort by date ASC (use sortByAsc instead of sortByDesc)
    – N69S
    Commented May 2, 2020 at 16:45
  • $inquiries = Inquiry::orderBy('created_at','desc')->orderBy('Status','desc')->get(); Commented May 2, 2020 at 16:49
  • @porloscerrosΨ Correct, date should be 'Create At' not "Date". I edited it.
    – Fly_Moe
    Commented May 2, 2020 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(); Commented May 2, 2020 at 17:38

2 Answers 2

14

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

sortBy() / sortByDesc()

The methods sortBy() and sortByDesc() are used to sort the data after fetching results from the database.

orderBy()

The method orderBy is the same as the SQL ORDER BY statement. This will sort the database records and 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();

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

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.