Syntax for SQL ORDER BY Keyword.
SELECT column_name list
FROM table_name
ORDER BY column1, column2, ... ASC|DESC
To learn more about SQL, please visit our BajarangiSoft site.
How can you use Order By statement in laravel
TheorderBy
method allows you to sort the result of the query by a given column. The first argument to the orderBy
method should be the column you wish to sort by, while the second argument controls the direction of the sort and may be either asc
or desc
:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;//include this
class UserController extends Controller
{
public function index()
{
$users = DB::table('users')->orderBy('name', 'desc')->get();
return view('user.index', ['users' => $users]);
}
}
?>