Laravel 5.1 对表格进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31704309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 5.1 sort a table
提问by Bhavnit34
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:
我有一个简单的控制器函数来输出用户表,但我希望它按“已批准”列排序,该列是 1 或 0。我的控制器如下所示:
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.
如何按此列对表格进行排序?我尝试过类似的功能,SortBy('approved','desc')
但这没有效果。
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
采纳答案by Wader
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
您可以使用查询构建器让数据库在检索结果之前对其进行排序。有关更多信息,请参阅文档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
或者,您也可以在使用 laravel 集合从数据库中检索它们后对它们进行排序。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'));
}
回答by Tom Elliott
You'll be looking for the orderBy method: http://laravel.com/docs/5.1/queries#ordering-grouping-limit-and-offset
您将寻找 orderBy 方法:http: //laravel.com/docs/5.1/queries#ordering-grouping-limit-and-offset
In this case:
在这种情况下:
$users = \User::orderBy('approved','desc')->get();