PHPackages                             s-damian/larasort - 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. s-damian/larasort

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

s-damian/larasort
=================

Column sortable for Laravel

v2.0.0(1mo ago)2612.8k↓23.7%1MITPHPPHP ^8.4CI passing

Since Aug 25Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/s-damian/larasort)[ Packagist](https://packagist.org/packages/s-damian/larasort)[ RSS](/packages/s-damian-larasort/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (10)Versions (25)Used By (0)

[![](https://raw.githubusercontent.com/s-damian/medias/main/package-logos/larasort.png)](https://github.com/s-damian/larasort)

Column Sorting for Laravel - Sortable - Sort By
===============================================

[](#column-sorting-for-laravel---sortable---sort-by)

[![Tests](https://github.com/s-damian/larasort/actions/workflows/tests.yml/badge.svg)](https://github.com/s-damian/larasort/actions/workflows/tests.yml)[![Total Downloads](https://camo.githubusercontent.com/3dfb04be501973e70b15989a35b2883f4e90aca4de0317dda034ea5d318754cd/68747470733a2f2f706f7365722e707567782e6f72672f732d64616d69616e2f6c617261736f72742f646f776e6c6f616473)](https://packagist.org/packages/s-damian/larasort)[![Latest Stable Version](https://camo.githubusercontent.com/4294cd629f40cf71046e12499cfad756722cefc2a1cc590b5d4194c353b323aa/68747470733a2f2f706f7365722e707567782e6f72672f732d64616d69616e2f6c617261736f72742f762f737461626c65)](https://packagist.org/packages/s-damian/larasort)[![License](https://camo.githubusercontent.com/e11a1aec687874929de77c2f657a151dde26c8f3207f48a68c9bf6b9ff62cfed/68747470733a2f2f706f7365722e707567782e6f72672f732d64616d69616e2f6c617261736f72742f6c6963656e7365)](https://packagist.org/packages/s-damian/larasort)

Larasort : Column Sorting For Laravel - Sort Easily
---------------------------------------------------

[](#larasort--column-sorting-for-laravel---sort-easily)

### Introduction - Larasort

[](#introduction---larasort)

This package allows you to automate the `ORDER BY` of your SQL queries, as well as to automate the generation of sortable links.

This Open Source library allows you to create **sortable columns** in an automated way with **Laravel**.

You have two packages in one: **Larasort** (for sorting **with** Eloquent ORM) and **LarasortManual** (for sorting **without** Eloquent ORM).

> Sort easily in an automated way 🚀

### Basic Example

[](#basic-example)

- Example in Model:

```
private array $sortables = [ // The attributes that are sortable.
    'email',
    'first_name',
    'created_at',
];
```

- Example in Controller:

```
$customers = Customer::whereNotNull('confirmed_at')
    ->autosort() // Automate ORDER BY and its direction.
    ->paginate();
```

- Example in View (in blade template):

```
@sortableLink('email', 'Email')
```

Example rendering of a link in a table:

[![Larasort](https://raw.githubusercontent.com/s-damian/medias/main/packages/larasort-th-example.webp)](https://github.com/s-damian/larasort)

### Author

[](#author)

This package is developed by [Stephen Damian](https://github.com/s-damian).

### Requirements

[](#requirements)

- PHP `8.4` || `8.5`
- Laravel `12` || `13`

If you are using an older version of PHP or Laravel: [1.x branch](https://github.com/s-damian/larasort/tree/1.x).

Summary
-------

[](#summary)

- [Installation](#installation)
- [Customization with "vendor:publish"](#customization-with-vendorpublish)
- [Larasort - For Eloquent ORM](#larasort---for-eloquent-orm)
    - [Basic usage](#basic-usage)
    - [Aliasing](#aliasing)
        - [Example with -&gt;join()](#example-with--join)
    - [Relationships](#relationships)
        - [One To One](#one-to-one)
        - [One To Many](#one-to-many)
        - [Belongs To](#belongs-to)
        - [Relationships - Conventions](#relationships---conventions)
    - [For a column, specify its table](#for-a-column-specify-its-table)
    - [Put "desc" or "asc" by default for some columns](#put-desc-or-asc-by-default-for-some-columns)
    - [Clear Larasort static methods](#clear-larasort-static-methods)
    - [Larasort - API Doc](#larasort---api-doc)
        - [Model properties](#model-properties)
        - [Larasort class](#larasort-class)
        - [AutoSortable trait](#autosortable-trait)
        - [Blade directives](#blade-directives)
- [LarasortManual - For without Eloquent ORM](#larasortmanual---for-without-eloquent-orm)
    - [LarasortManual - Basic usage](#larasortmanual---basic-usage)
    - [LarasortManual - For a column, specify its table](#larasortmanual---for-a-column-specify-its-table)
    - [LarasortManual - Put "desc" or "asc" by default for some columns](#larasortmanual---put-desc-or-asc-by-default-for-some-columns)
    - [LarasortManual - API Doc](#larasortmanual---api-doc)
        - [LarasortManual class](#larasortmanual-class)
- [Support](#support)
- [License](#license)

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

[](#installation)

Installation via Composer:

```
composer require s-damian/larasort

```

Or directly the *2.x* branch:

```
composer require s-damian/larasort:^2

```

Customization With "vendor:publish"
-----------------------------------

[](#customization-with-vendorpublish)

### Custom Config and Lang and CSS

[](#custom-config-and-lang-and-css)

After installing the package, you have to run the `vendor:publish` command:

```
php artisan vendor:publish --provider="SDamian\Larasort\LarasortServiceProvider"

```

The `vendor:publish` command will generate these files:

- `config/larasort.php`
- `public/vendor/larasort/css/larasort.css` (**you must include this CSS in your website**)
- `public/vendor/larasort/images/order.webp`

You can of course customize these files.

### "vendor:publish" with "--tag" argument

[](#vendorpublish-with---tag-argument)

Publish only `config` file:

```
php artisan vendor:publish --provider="SDamian\Larasort\LarasortServiceProvider" --tag=config

```

Publish only `CSS` file:

```
php artisan vendor:publish --provider="SDamian\Larasort\LarasortServiceProvider" --tag=css

```

Publish only `images` file:

```
php artisan vendor:publish --provider="SDamian\Larasort\LarasortServiceProvider" --tag=images

```

Larasort - For Eloquent ORM
===========================

[](#larasort---for-eloquent-orm)

**Larasort** is useful when using the Eloquent ORM.

Basic Usage
-----------

[](#basic-usage)

First, your Model must use the `AutoSortable` Trait.

Then it is necessary that in your Model you declare `$sortables`. This property is useful for defining the columns (columns in your DB table) allowed to be sorted in the `ORDER BY`.

Note: the first column of the array `$sortables` will be the column used by default for the SQL `ORDER BY`.

Example:

```
