Get All Rows With Eloquent and Order Them

Eloquent can replace all of your MySQL code and help you work much faster, but it becomes confusing sometimes. Now I will show you how to get all rows from the database and order them in just one line.

Most people would be tempted to use Model::all() and then use the orderBy function, but it's wrong.

How to retrieve all rows from the database and order them with Eloquent

Like I've said, it can be done with a single line of code which you can read below:

$rows = Model::orderBy('column')->get();

You don't have to use all() unless you want to retrieve everything unchanged. Otherwise you can start directly with orderBy, where or almost any other Eloquent function.