laravel sort tables

VIEW

<table class=”table table-striped table-bordered”>

<thead>
<tr>
<td>ID</td>
<td>name</td>
<td>email</td>
<!–<td>{{ link_to_route(‘reviews.index’, ‘rating’, [‘sort’ => ‘rating’,’direction’=>$direction]) }}</td>–>
<td>{{sortableColumn(‘reviews.index’, ‘rating’, ‘rating’, $direction)}}</td>
<td>{{sortableColumn(‘reviews.index’, ‘section’, ‘section’, $direction)}}</td>
<td>comments</td>
<td></td>
<td>actions</td>
</tr>
</thead>

———————————————————————–

CONTROLLER

public function index() {

$sort = null !==(Input::get(‘sort’))? Input::get(‘sort’) : ‘id’ ;

$direction = null !==(Input::get(‘direction’))? Input::get(‘direction’) : ‘DESC’ ;

$reviews = Review::orderBy($sort, $direction)->paginate(10);

$direction = $direction==’DESC’? ‘ASC’: ‘DESC’ ;

return View::make(‘reviews.index’, compact(‘reviews’,’direction’));
}

Leave a comment