Laravel 8 Order By Query Example

SELECT column1, column2
FROM table_name
ORDER BY column1, column2 ASC|DESC;
SELECT * FROM users
ORDER BY name;
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
User::orderBy('name', 'DESC')
->orderBy('email', 'ASC')
->get();
User::orderBy('created_at', 'DESC')->get();User::orderBy('updated_at', 'ASC')->get();
$users = User::with(['posts' => function ($query) {
$query->orderBy('updated_at', 'asc');
}])->get();
$users = User::orderBy('id', 'desc')->limit(7)->get(); 

$users = User::orderBy('id', 'asc')->limit(7)->get();

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store