How to sort twice with two columns in Laravel?

eloquent, laravel, laravel-5, laravel-5.4, php

Solution

You shouldn't sort on collection but you should get sorted results from database like this:

$articles = Auth::user()->articles()
              ->orderBy('updated_at', 'DESC')
              ->orderBy('status','DESC')
              ->get();

To explain a bit more - in your code you got all results from database (in random order) and then tried to sort all the results.

In code I presented you sort results in database so you can easily sort on mutliple columns.

Notice difference in my code - instead of `->articles` I used `->articles()` and added `->get()` at the end to get results.

Problem

I am using Laravel 5.4, How to sort twice with two columns? For example: `ArticleController.php` ``` public function index() { $articles = Auth::user()->articles->sortByDesc('updated_at')->sortByDesc('status'); return view('index', compact('articles')); } ``` I use `sortByDesc()` twice,but the result does not follow the rules,what should I do? update: `User.php` ``` public function articles() { return $this->hasMany('App\Article'); } ```

Original source