Reputation: 133
I am new to laravel, and i try to orderBy
the rand
, is it possible to order by in each column ? my code just take row rand but it dont shuffle the column just the row.
public function show(){
$name = DB::table('names')
->inRandomOrder()
->get();
return view('content', ['names' => $names]);
}
Upvotes: 2
Views: 1707
Reputation: 190
orderBy in Laravel.
The orderBy 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:
$users = DB::table('users')
->orderBy('name', 'desc')
->get();
In your case this will work.
public function show(){
$names = DB::table('names')
->orderBy('name', 'desc')
->orderBy('city', 'asc')
->get();
return view('content', ['names' => $names]);
Upvotes: 4
Reputation: 5092
You Can Do Like This For To Order by in each column
public function show(){
$names = DB::table('names')
->orderBy('name', 'DESC')
->orderBy('city', 'DESC')
->orderBy('email', 'ASC')
->get();
return view('content', ['names' => $names]);
}
With
foreach
$names = DB::table('names');
foreach ($request->get('order_by_columns') as $column => $direct) {
$names->orderBy($column, $direct);
}
$results = $names->get();
Upvotes: 0