Reputation: 15
I have a simple controller function to output a table of users, but I want it to sort by the 'approved' column, which is either 1 or 0. My controller looks like this:
public function showAllUsers()
{
$users = \User::all();
return \View::make('admin.users.all', array('users' => $users));
}
How do I sort the table by this column? I have tried functions like SortBy('approved','desc')
but this doesn't have an effect.
View:*
@extends('layouts.admin')
@section('scripts')
<script type="text/javascript" src="../js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="../js/datatables.js"></script>
@stop
@section('stylesheets')
<link rel="stylesheet" type="text/css" href="{{url('css/datatables.css')}}" />
@stop
@section('content')
<div class="row">
<div class="col-lg-12">
<h1>Members</h1>
@if(Session::has('success'))
<div class="alert alert-success">
<p>{{
Session::get('success') }}</p>
</div>
@endif
</div>
<div class="col-lg-12">
<a type="button" class="btn btn-success" href="{{url('admin/members/add')}}">Add Member</a>
<br/>
<br/>
</div>
</div><!-- /.row -->
<div class="row">
<div class="col-lg-12">
<div class="table-responsive">
<table id="dattab" class="table table-hover table-striped tablesorter">
<thead>
<tr>
<th class="header headerSortUp">Membership ID <i class="fa fa-sort"></i></th>
<th class="header">Fullname <i class="fa fa-sort"></i></th>
<th class="header">Membership Type <i class="fa fa-sort"></i></th>
<th class="header">Status<i class="fa fa-sort"></i></th>
<th class="header">Join Date<i class="fa fa-sort"></i></th>
<th class="header">Actions</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr>
<td>{{$user->membership_id}}</td>
<td>{{$user->firstname." ".$user->lastname}}</td>
<td>{{$user->getMembershipType()}}</td>
<td>
@if($user->approved) Approved
@elseif (!$user->approved)
<a href="{{url('admin/members/approve/'.$user->id)}}" class="btn btn-success">Approve</a>
@endif
</td>
<td>
{{ (new DateTime($user->created_at))->format('d/m/Y') }}
</td>
<td>
<div class="btn-group">
<button class="btn btn-primary btn-sm dropdown-toggle" type="button" data-toggle="dropdown">
Choose Action <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a href="{{url('admin/members/edit/'.$user->id)}}">Edit</a></li>
<li class="divider"></li>
<li><a href="{{url('admin/members/delete/'.$user->id)}}" onclick="return confirm('Are you sure');">Delete</a></li>
</ul>
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div><!-- /.row -->
@stop
Upvotes: 1
Views: 8550
Reputation: 9883
You can use the query builder to get the database to order the results before they're retrieved. See the docs for more information http://laravel.com/docs/5.1/queries#ordering-grouping-limit-and-offset
public function showAllUsers()
{
$users = \User::orderBy('approved', 'desc')->get();
return view('admin.users.all', compact('users'));
}
Alternativly you can also sort them after they've been retrieved from the database using the laravel collections. http://laravel.com/docs/5.1/collections#method-sortby
public function showAllUsers()
{
$users = \User::all()->sortBy('approved', 'desc');
return view('admin.users.all', compact('users'));
}
Upvotes: 1
Reputation: 1926
You'll be looking for the orderBy method: http://laravel.com/docs/5.1/queries#ordering-grouping-limit-and-offset
In this case:
$users = \User::orderBy('approved','desc')->get();
Upvotes: 0