PHPackages                             akaunting/laravel-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. [Database &amp; ORM](/categories/database)
4. /
5. akaunting/laravel-sortable

ActiveLibrary[Database &amp; ORM](/categories/database)

akaunting/laravel-sortable
==========================

Sortable behavior package for Laravel

3.0.0(1mo ago)27195.7k↑17.1%7MITPHPPHP ^8.2CI passing

Since Dec 30Pushed 1mo ago4 watchersCompare

[ Source](https://github.com/akaunting/laravel-sortable)[ Packagist](https://packagist.org/packages/akaunting/laravel-sortable)[ RSS](/packages/akaunting-laravel-sortable/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (10)Dependencies (6)Versions (12)Used By (0)

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

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

[![Downloads](https://camo.githubusercontent.com/77699c16af47005fec01aa1821d3916ca30ade239cfe7fc9ebcd6404c1b03b5b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616b61756e74696e672f6c61726176656c2d736f727461626c65)](https://camo.githubusercontent.com/77699c16af47005fec01aa1821d3916ca30ade239cfe7fc9ebcd6404c1b03b5b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616b61756e74696e672f6c61726176656c2d736f727461626c65)[![Tests](https://camo.githubusercontent.com/27152ec6a1c4ffa7ba5c0134d0d726df567271f95939c0d090fa11af5a9e4edd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616b61756e74696e672f6c61726176656c2d736f727461626c652f74657374732e796d6c3f6c6162656c3d7465737473)](https://camo.githubusercontent.com/27152ec6a1c4ffa7ba5c0134d0d726df567271f95939c0d090fa11af5a9e4edd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f616b61756e74696e672f6c61726176656c2d736f727461626c652f74657374732e796d6c3f6c6162656c3d7465737473)[![StyleCI](https://camo.githubusercontent.com/c7716b74cbbd6d376ba05e4c7b1b2d8c3b049a4744077088eb3a64b6a390ff2b/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3434323237313934322f736869656c643f7374796c653d666c6174266272616e63683d6d6173746572)](https://styleci.io/repos/442271942)[![License](https://camo.githubusercontent.com/81357cd675ad031d34dd85156540cecab4ff5d2bbbc7a395d3ac4d0eceed051f/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f616b61756e74696e672f6c61726176656c2d736f727461626c65)](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 akaunting/laravel-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 Akaunting\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/akaunting/laravel-sortable/security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [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

59

—

FairBetter than 98% of packages

Maintenance90

Actively maintained with recent releases

Popularity45

Moderate usage in the ecosystem

Community17

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 69.6% 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 ~160 days

Recently: every ~333 days

Total

11

Last Release

47d ago

Major Versions

1.0.6 → 2.0.02023-03-04

2.0.2 → 3.0.02026-05-17

PHP version history (3 changes)1.0.0PHP &gt;=7.3

2.0.0PHP ^8.0

3.0.0PHP ^8.2

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/5254835?v=4)[Denis Dulici](/maintainers/denisdulici)[@denisdulici](https://github.com/denisdulici)

![](https://avatars.githubusercontent.com/u/10936547?v=4)[Cüneyt Şentürk](/maintainers/cuneytsenturk)[@cuneytsenturk](https://github.com/cuneytsenturk)

---

Top Contributors

[![denisdulici](https://avatars.githubusercontent.com/u/5254835?v=4)](https://github.com/denisdulici "denisdulici (16 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)")

---

Tags

bladeeloquentlaravelphpsortabletraitlaravelmodelbladesortablesortviewsortingcolumn

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/akaunting-laravel-sortable/health.svg)

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

###  Alternatives

[spatie/eloquent-sortable

Sortable behaviour for eloquent models

1.5k25.7M319](/packages/spatie-eloquent-sortable)[rutorika/sortable

Adds sortable behavior and ordering to Laravel Eloquent models. Grouping and many to many supported.

2891.1M17](/packages/rutorika-sortable)[jedrzej/pimpable

Laravel 4/5/6 package that allows to dynamically filter, sort and eager load relations for your models using request parameters

105188.2k1](/packages/jedrzej-pimpable)[indexzer0/eloquent-filtering

Powerful eloquent filtering

22532.2k5](/packages/indexzer0-eloquent-filtering)[jedrzej/sortable

Sortable trait for Laravel's Eloquent models - sort your models using request parameters

54279.0k1](/packages/jedrzej-sortable)

PHPackages © 2026

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