PHPackages                             rogercbe/table-sorter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. rogercbe/table-sorter

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

rogercbe/table-sorter
=====================

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

v1.1.2(8y ago)5643MITPHPPHP &gt;=5.6.4

Since Aug 16Pushed 8y ago1 watchersCompare

[ Source](https://github.com/rogercbe/LaravelTableSorter)[ Packagist](https://packagist.org/packages/rogercbe/table-sorter)[ Docs](https://github.com/rogercbe/laravel-table-sorter)[ RSS](/packages/rogercbe-table-sorter/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (4)DependenciesVersions (6)Used By (0)

Laravel Table Sorter
====================

[](#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
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Usage](#usage)
    - [Sort Model Attributes](#sort-model-attributes)
    - [Sort Model Relationships](#sort-model-relationships)
    - [Sort Model Count Relationships](#sort-model-count-relationships)
    - [Sort Links Helper](#sort-links-helper)
- [Contributing](#contributing)
- [License](#license)

Installation
------------

[](#installation)

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

```
Rogercbe\TableSorter\TableSorterServiceProvider::class,
```

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.

Usage
-----

[](#usage)

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: `your-site.dev/?sort={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.

```
your-site.dev/?sort={COLUMN}&direction={asc|desc}

```

### Sort Model Attributes

[](#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.

```
your-site.dev/?sort=name&direction=asc

```

### Sort Model Relationships

[](#sort-model-relationships)

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

```
your-site.dev/?sort=company.name&direction=asc

```

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

[](#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.

```
your-site.dev/?sort=posts_count&direction=asc

```

```
$users = User::withCount('posts')->sortable()->paginate();
```

### Sort Links Helper

[](#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()
            ->sortPaginate();
        ...
    }
...
```

Then in your view:

```

        {{ $users->sortLinks() }}

        @foreach($users as $user)

                {{ $user->company->name }}
                {{ $user->name }}

        @endforeach

{{ $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.

Contributing
------------

[](#contributing)

You are more than welcome to contribute to the package by submitting a [Pull Request](https://github.com/rogercbe/LaravelTableSorter/pulls).

License
-------

[](#license)

The MIT License (MIT). Please see [License File](https://github.com/rogercbe/LaravelTableSorter/blob/master/License) for more information.

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity18

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~50 days

Total

4

Last Release

3040d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4433802a6b66efe8fc02c6071e21728272b917a23ea22c72020613a2d63767dd?d=identicon)[rogercbe](/maintainers/rogercbe)

---

Top Contributors

[![rogercbe](https://avatars.githubusercontent.com/u/9057214?v=4)](https://github.com/rogercbe "rogercbe (12 commits)")

---

Tags

laravelphptable-sortinglaraveltablesorter

### Embed Badge

![Health badge](/badges/rogercbe-table-sorter/health.svg)

```
[![Health](https://phpackages.com/badges/rogercbe-table-sorter/health.svg)](https://phpackages.com/packages/rogercbe-table-sorter)
```

###  Alternatives

[okipa/laravel-table

Generate tables from Eloquent models.

56752.8k](/packages/okipa-laravel-table)[ramonrietdijk/livewire-tables

Dynamic tables for models with Laravel Livewire

21147.4k](/packages/ramonrietdijk-livewire-tables)[gbrock/laravel-table

Table functionality for Laravel models

7644.3k](/packages/gbrock-laravel-table)[awes-io/table-builder

A component that allows creating responsive HTML tables or lists from data object

4726.1k4](/packages/awes-io-table-builder)[optimistdigital/nova-resizable

Simple Laravel Nova tool to enable column resizing

1818.9k](/packages/optimistdigital-nova-resizable)[tomshaw/electricgrid

A feature-rich Livewire package designed for projects that require dynamic, interactive data tables.

116.6k](/packages/tomshaw-electricgrid)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
