rogercbe / LaravelTableSorter by rogercbe

A package to easily add sorting functionality to any of your models along with helpers to create the sorting links.
Package Data
Maintainer Username: rogercbe
Maintainer Contact: (Roger Cendrós)
Package Create Date: 2017-08-16
Package Last Update: 2018-01-15
Home Page:
Language: PHP
License: MIT
Last Refreshed: 2020-08-09 15:16:20
Package Statistics
Total Downloads: 558
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 4
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

Laravel Table Sorter

This package easily add sorting functionality to any of your models along with helpers to create the sorting links.

Table of Contents


Pull this package through Composer.

composer require rogercbe/table-sorter

After the instalation add the ServiceProvider to the providers array in your config/app.php file


Finally to publish the table header view use:

php artisan vendor:publish

This will create a table-sorter folder under your resources/views/vendor directory with the table header view.


To start using this package you only have to use the Sortable trait on the model you wish to allow sorting. This Trait allows you to use the sortable which will listen to the GET request parameters and execute the queries needed to sort the records.

use Rogercbe\TableSorter\Sortable;

class User extends Authenticatable
	use Sortable;

On your controller scope your query to listen for the sorting using sortable method

use App\User;

class UsersController extends Controller
	public function index()
        $users = User::sortable()->get()

This method will respond to urls following the convention below:{COLUMN-TO-SORT}&direction={asc/desc} To build the url you must specify the column name that has to be sorted and the direction, the direction parameter is optional, by default the direction will be ascending.{COLUMN}&direction={asc|desc}

Sort Model Attributes

In order to sort by model attributes you have to specify the column name which should be sorted on the sort request variable.

Sort Model Relationships

In order to sort by model relationships the sort variable has to be set using this convention relation.column_name.

There is no limit on nested relationships levels, it will perform the necessary join queries to be able to sort by the attribute selected.

Sort Model Count Relationships

In order to sort by count relationships you must perform a withCount('relation') before calling sortable so Laravel can eager load the query the count relationship and attach it to the model. This way you will have avaiable the relation_count variable on the model and we can sort by it, specially useful in case that count has to be constrained.
$users = User::withCount('posts')->sortable()->paginate();

Sort Links Helper

If you wish to generate the pagination and table header links, this package allows to define the table headers and their options on your model and render them.

use Rogercbe\TableSorter\Sortable;

class User extends Authenticatable
    use Sortable;
    protected $tableHeaders = [
        'name' => [
        	'title' => 'User Name'
        'email' => [
        	'class' => ['my-class', 'another-class']
        'created_at' => [
			'sortable' => 'false',
			'class' => 'one-class'

By default all headers are sortable, so you don't need to specify that in the headers configuration, only specify the ones that should be disabled. The title can be ommited aswell, by default it will capitalize the column name. If you wish to add certain classes to the header selector, you can pass a string or an array of strings containing the classes that should be added as the example shows.

You'll need to call sortPaginate() or sortSimplePaginate() methods instead of laravel's paginate() and simplePaginate() in order to use the helper functions to render the paginator and the table header, to render those links you only need to call the sortLinks() method in your view.

In your controller:

	public function index()
        $users = User::sortable()

Then in your view:

        {{ $users->sortLinks() }}
        @foreach($users as $user)
                <td>{{ $user->company->name }}</td>
                <td>{{ $user->name }}</td>
{{ $users->pagination() }}

The method sortLinks() is a helper that will render the table headers that you specified with basic functionality, creating the links to sort ascending or descending aswell as inserting arrows to show the direction. The default view can be published to edit it, or you can specify your own by creating a sortLinksView property on your model. It accepts a path to a view as a parameter aswell.

The method pagination() delegates to links() method from laravel paginator appending the get request values needed to sort. You are free to use either, and append the values yourself, it is just a helper.


You are more than welcome to contribute to the package by submitting a Pull Request.


The MIT License (MIT). Please see License File for more information.