PHPackages                             jesprna/jaysfi-sortable - 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. [Templating &amp; Views](/categories/templating)
4. /
5. jesprna/jaysfi-sortable

ActiveLibrary[Templating &amp; Views](/categories/templating)

jesprna/jaysfi-sortable
=======================

Sortable behavior package for Laravel

v1.0.1(3mo ago)0427↓50%MITPHPPHP ^8.0

Since Jan 10Pushed 3mo agoCompare

[ Source](https://github.com/jesprna/jaysfi-sortable)[ Packagist](https://packagist.org/packages/jesprna/jaysfi-sortable)[ RSS](/packages/jesprna-jaysfi-sortable/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (3)Versions (3)Used By (0)

Sortable behavior package for Laravel
=====================================

[](#sortable-behavior-package-for-laravel)

[![Downloads](https://camo.githubusercontent.com/10872142986e0907293588eb36b3bdf283e6ae01d96f443c70e55d8d9f554718/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a657370726e612f6a61797366692d736f727461626c65)](https://camo.githubusercontent.com/10872142986e0907293588eb36b3bdf283e6ae01d96f443c70e55d8d9f554718/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a657370726e612f6a61797366692d736f727461626c65)[![Tests](https://camo.githubusercontent.com/d794068c56323c2fbcb89bb79fab950476e7c1d1c7a8c20329b6f0b34da7582f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a657370726e612f6a61797366692d736f727461626c652f74657374732e796d6c3f6c6162656c3d7465737473)](https://camo.githubusercontent.com/d794068c56323c2fbcb89bb79fab950476e7c1d1c7a8c20329b6f0b34da7582f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a657370726e612f6a61797366692d736f727461626c652f74657374732e796d6c3f6c6162656c3d7465737473)[![StyleCI](https://camo.githubusercontent.com/c7716b74cbbd6d376ba05e4c7b1b2d8c3b049a4744077088eb3a64b6a390ff2b/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3434323237313934322f736869656c643f7374796c653d666c6174266272616e63683d6d6173746572)](https://styleci.io/repos/442271942)[![License](https://camo.githubusercontent.com/7c0ab1db698bff527b09ecaba7cf025ded9782f870857978461b28af95be3c07/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6a657370726e612f6a61797366692d736f727461626c65)](LICENSE.md)

This package allows you to add sortable behavior to `models` and `views`. It ships with a trait where you can set the sortable fields and a blade directive to generate table headers automatically.

Getting Started
---------------

[](#getting-started)

### 1. Install

[](#1-install)

Run the following command:

```
composer require jesprna/jaysfi-sortable
```

### 2. Publish

[](#2-publish)

Publish configuration

```
php artisan vendor:publish --tag=sortable
```

### 3. Configure

[](#3-configure)

You can change the column sorting settings of your app from `config/sortable.php` file

Usage
-----

[](#usage)

All you have to do is use the `Sortable` trait inside your model and define the `$sortable` fields.

```
use Jesprna\Sortable\Traits\Sortable;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use Sortable;
    ...

    public $sortable = [
        'id',
        'title',
        'author',
        'created_at',
    ];
    ...
}
```

If you don't define the `$sortable` array, the `Scheme::hasColumn()` function is used which runs an extra database query.

### Scope

[](#scope)

The trait adds a `sortable` scope to the model so you can use it just before `paginate`:

```
public function index()
{
    $posts = Post::query()->sortable()->paginate(10);

    return view('posts.index')->with(['posts' => $posts]);
}
```

You can set also default sorting field which will be applied when URL is empty.

```
$posts = $post->sortable(['author'])->paginate(10); // $post->orderBy('posts.author', 'asc')

$posts = $post->sortable(['title'])->paginate(10); // $post->orderBy('posts.title', 'asc')

$posts = $post->sortable(['title' => 'desc'])->paginate(10); // $post->orderBy('posts.title', 'desc')
```

### Blade Directive

[](#blade-directive)

There is also a `blade` directive for you to create sortable links in your views:

```
@sortablelink('title', trans('general.title'), ['parameter' => 'smile'],  ['rel' => 'nofollow'])
```

The *first* parameter is the column in database. The *second* one is displayed inside the anchor tag. The *third* one is an `array()`, and it sets the default (GET) query string. The *fourth* one is also an `array()` for additional anchor-tag attributes. You can use a custom URL as 'href' attribute in the fourth parameter, which will append the query string.

Only the first parameter is required.

Examples:

```
@sortablelink('title')
@sortablelink('title', trans('general.title'))
@sortablelink('title', trans('general.title'), ['filter' => 'active, visible'])
@sortablelink('title', trans('general.title'), ['filter' => 'active, visible'], ['class' => 'btn btn-success', 'rel' => 'nofollow', 'href' => route('posts.index')])
```

#### Icon Set

[](#icon-set)

You can use any icon set you want. Just change the `icons.wrapper` from the config file accordingly. By default, it uses Font Awesome.

### Blade Component

[](#blade-component)

Same as the directive, there is also a `blade` component for you to create sortable links in your views:

```

```

### Sorting Relationships

[](#sorting-relationships)

The package supports `HasOne` and `BelongsTo` relational sorting:

```
class Post extends Model
{
    use Sortable;
    ...

    protected $fillable = [
        'title',
        'author_id',
        'body',
    ];

    public $sortable = [
        'id',
        'title',
        'author',
        'created_at',
        'updated_at',
    ];

    /**
    * Get the author associated with the post.
    */
    public function author()
    {
        return $this->hasOne(\App\Models\Author::class);
    }
    ...
}
```

And you can use the relation in views:

```
// resources/views/posts/index.blade.php

@sortablelink('title', trans('general.title'))
@sortablelink('author.name', trans('general.author'))
```

> **Note**: In case there is a self-referencing model (like comments, categories etc.); parent table will be aliased with `parent_` string.

### Advanced Relation

[](#advanced-relation)

You can also extend the relation sorting feature by creating a function with `Sortable` suffix. There you're free to write your own queries and apply `orderBy()` manually:

```
class User extends Model
{
    use Sortable;
    ...

    public $sortable = [
        'name',
        'address',
    ];

    public function addressSortable($query, $direction)
    {
        return $query->join('user_details', 'users.id', '=', 'user_details.user_id')
                    ->orderBy('address', $direction)
                    ->select('users.*');
    }
    ...
```

The usage in `controller` and `view` remains the same.

### Aliasing

[](#aliasing)

You can declare the `$sortableAs` array in your model and use it to alias (bypass column exists check), and ignore prefixing with table:

```
public $sortableAs = [
    'nick_name',
];
```

In controller

```
$users = $user->select(['name as nick_name'])->sortable(['nick_name'])->paginate(10);
```

In view

```
@sortablelink('nick_name', 'nick')
```

It's very useful when you want to sort results using [`withCount()`](https://laravel.com/docs/eloquent-relationships#counting-related-models).

Changelog
---------

[](#changelog)

Please see [Releases](../../releases) for more information what has changed recently.

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

[](#contributing)

Pull requests are more than welcome. You must follow the PSR coding standards.

Security
--------

[](#security)

Please review [our security policy](https://github.com/jesprna/jaysfi-sortable/security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [Jays](https://github.com/jesprna)
- [Denis Duliçi](https://github.com/denisdulici)
- [Martin Kiesel](https://github.com/Kyslik)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [LICENSE](LICENSE.md) for more information.

###  Health Score

39

—

LowBetter than 85% of packages

Maintenance83

Actively maintained with recent releases

Popularity17

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity40

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 62.5% 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 ~0 days

Total

2

Last Release

119d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/999042c0244396cbd9fbee3654cbc6ab436604538a3b3daac92505b4bfbec06f?d=identicon)[jesprna](/maintainers/jesprna)

---

Top Contributors

[![denisdulici](https://avatars.githubusercontent.com/u/5254835?v=4)](https://github.com/denisdulici "denisdulici (15 commits)")[![cuneytsenturk](https://avatars.githubusercontent.com/u/10936547?v=4)](https://github.com/cuneytsenturk "cuneytsenturk (5 commits)")[![CihanSenturk](https://avatars.githubusercontent.com/u/53110792?v=4)](https://github.com/CihanSenturk "CihanSenturk (2 commits)")[![jesprna](https://avatars.githubusercontent.com/u/48647209?v=4)](https://github.com/jesprna "jesprna (2 commits)")

---

Tags

laravelmodelbladesortablesortviewsortingcolumn

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jesprna-jaysfi-sortable/health.svg)

```
[![Health](https://phpackages.com/badges/jesprna-jaysfi-sortable/health.svg)](https://phpackages.com/packages/jesprna-jaysfi-sortable)
```

###  Alternatives

[akaunting/laravel-sortable

Sortable behavior package for Laravel

27175.6k](/packages/akaunting-laravel-sortable)[kyslik/column-sortable

Package for handling column sorting in Laravel 6.x

6485.6M21](/packages/kyslik-column-sortable)[sinnbeck/laravel-dom-assertions

106250.5k8](/packages/sinnbeck-laravel-dom-assertions)[leitsch/kirby-blade

Enable Laravel Blade Template Engine for Kirby 4 and Kirby 5

219.2k](/packages/leitsch-kirby-blade)

PHPackages © 2026

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