{"id":14974963,"url":"https://github.com/s-damian/larasort","last_synced_at":"2025-08-23T13:32:14.717Z","repository":{"id":59751073,"uuid":"528783957","full_name":"s-damian/larasort","owner":"s-damian","description":"Larasort : Column sorting with Laravel - Eloquent sortable","archived":false,"fork":false,"pushed_at":"2025-03-29T14:11:55.000Z","size":195,"stargazers_count":25,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-21T04:45:23.184Z","etag":null,"topics":["eloquent","eloquent-orm","laravel","laravel-library","laravel-package","laravel10","laravel11","laravel12","laravel8","laravel9","orderby","php8","php84","sortable","sortable-list","sorting","sorting-library","sorting-list"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/s-damian.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2022-08-25T09:37:47.000Z","updated_at":"2025-03-29T14:11:13.000Z","dependencies_parsed_at":"2024-02-24T08:29:27.755Z","dependency_job_id":"ffd8c2d4-2e1f-4155-aba4-c6ce4097a172","html_url":"https://github.com/s-damian/larasort","commit_stats":{"total_commits":105,"total_committers":3,"mean_commits":35.0,"dds":0.2666666666666667,"last_synced_commit":"8e297702196406fdde9b0d7ca3435f15e99c1236"},"previous_names":[],"tags_count":20,"template":false,"template_full_name":null,"purl":"pkg:github/s-damian/larasort","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-damian%2Flarasort","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-damian%2Flarasort/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-damian%2Flarasort/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-damian%2Flarasort/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/s-damian","download_url":"https://codeload.github.com/s-damian/larasort/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-damian%2Flarasort/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271749051,"owners_count":24814115,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-23T02:00:09.327Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","eloquent-orm","laravel","laravel-library","laravel-package","laravel10","laravel11","laravel12","laravel8","laravel9","orderby","php8","php84","sortable","sortable-list","sorting","sorting-library","sorting-list"],"created_at":"2024-09-24T13:51:19.542Z","updated_at":"2025-08-23T13:32:14.680Z","avatar_url":"https://github.com/s-damian.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n\u003ca href=\"https://github.com/s-damian/larasort\"\u003e\n\u003cimg src=\"https://raw.githubusercontent.com/s-damian/medias/main/package-logos/larasort.png\" width=\"400\"\u003e\n\u003c/a\u003e\n\u003c/p\u003e\n\n# Column Sorting for Laravel - Sortable - Sort By\n\n[![Tests](https://github.com/s-damian/larasort/actions/workflows/tests.yml/badge.svg)](https://github.com/s-damian/larasort/actions/workflows/tests.yml)\n[![Static analysis](https://github.com/s-damian/larasort/actions/workflows/static-analysis.yml/badge.svg)](https://github.com/s-damian/larasort/actions/workflows/static-analysis.yml)\n[![Total Downloads](https://poser.pugx.org/s-damian/larasort/downloads)](https://packagist.org/packages/s-damian/larasort)\n[![Latest Stable Version](https://poser.pugx.org/s-damian/larasort/v/stable)](https://packagist.org/packages/s-damian/larasort)\n[![License](https://poser.pugx.org/s-damian/larasort/license)](https://packagist.org/packages/s-damian/larasort)\n\n## Larasort : Column Sorting For Laravel - Sort Easily\n\n### Introduction - Larasort\n\nThis package allows you to automate the `ORDER BY` of your SQL queries, as well as to automate the generation of sortable links.\n\nThis Open Source library allows you to create **sortable columns** in an automated way with **Laravel**.\n\nYou have two packages in one: **Larasort** (for sorting **with** Eloquent ORM) and **LarasortManual** (for sorting **without** Eloquent ORM).\n\n\u003e Sort easily in an automated way 🚀\n\n### Basic Example\n\n- Example in Model:\n\n```php\nprivate array $sortables = [ // The attributes that are sortable.\n    'email',\n    'first_name',\n    'created_at',\n];\n```\n\n- Example in Controller:\n\n```php\n$customers = Customer::whereNotNull('confirmed_at')\n    -\u003eautosort() // Automate ORDER BY and its direction.\n    -\u003epaginate();\n```\n\n- Example in View (in blade template):\n\n```html\n@sortableLink('email', 'Email')\n```\n\nExample rendering of a link in a table:\n\n[![Larasort](https://raw.githubusercontent.com/s-damian/medias/main/packages/larasort-th-example.webp)](https://github.com/s-damian/larasort)\n\n### Author\n\nThis package is developed by [Stephen Damian](https://github.com/s-damian).\n\n### Requirements\n\n- PHP `8.0` || `8.1` || `8.2` || `8.3` || `8.4`\n- Laravel `8` || `9` || `10` || `11` || `12`\n\n\n## Summary\n\n- [Installation](#installation)\n- [Customization with \"vendor:publish\"](#customization-with-vendorpublish)\n- [Larasort - For Eloquent ORM](#larasort---for-eloquent-orm)\n  - [Basic usage](#basic-usage)\n  - [Aliasing](#aliasing)\n    - [Example with -\u003ejoin()](#example-with--join)\n  - [Relationships](#relationships)\n    - [One To One](#one-to-one)\n    - [One To Many](#one-to-many)\n    - [Belongs To](#belongs-to)\n    - [Relationships - Conventions](#relationships---conventions)\n  - [For a column, specify its table](#for-a-column-specify-its-table)\n  - [Put \"desc\" or \"asc\" by default for some columns](#put-desc-or-asc-by-default-for-some-columns)\n  - [Clear Larasort static methods](#clear-larasort-static-methods)\n  - [Larasort - API Doc](#larasort---api-doc)\n    - [Model properties](#model-properties)\n    - [Larasort class](#larasort-class)\n    - [AutoSortable trait](#autosortable-trait)\n    - [Blade directives](#blade-directives)\n- [LarasortManual - For without Eloquent ORM](#larasortmanual---for-without-eloquent-orm)\n  - [LarasortManual - Basic usage](#larasortmanual---basic-usage)\n  - [LarasortManual - For a column, specify its table](#larasortmanual---for-a-column-specify-its-table)\n  - [LarasortManual - Put \"desc\" or \"asc\" by default for some columns](#larasortmanual---put-desc-or-asc-by-default-for-some-columns)\n  - [LarasortManual - API Doc](#larasortmanual---api-doc)\n    - [LarasortManual class](#larasortmanual-class)\n- [Support](#support)\n- [License](#license)\n\n\n## Installation\n\nInstallation via Composer:\n\n```\ncomposer require s-damian/larasort\n```\n\n\n## Customization With \"vendor:publish\"\n\n### Custom Config and Lang and CSS\n\nAfter installing the package, you have to run the `vendor:publish` command:\n\n```\nphp artisan vendor:publish --provider=\"SDamian\\Larasort\\LarasortServiceProvider\"\n```\n\nThe `vendor:publish` command will generate these files:\n\n- `config/larasort.php`\n- `public/vendor/larasort/css/larasort.css` (**you must include this CSS in your website**)\n- `public/vendor/larasort/images/order.webp`\n\nYou can of course customize these files.\n\n### \"vendor:publish\" with \"--tag\" argument\n\nPublish only `config` file:\n\n```\nphp artisan vendor:publish --provider=\"SDamian\\Larasort\\LarasortServiceProvider\" --tag=config\n```\n\nPublish only `CSS` file:\n\n```\nphp artisan vendor:publish --provider=\"SDamian\\Larasort\\LarasortServiceProvider\" --tag=css\n```\n\nPublish only `images` file:\n\n```\nphp artisan vendor:publish --provider=\"SDamian\\Larasort\\LarasortServiceProvider\" --tag=images\n```\n\n\n# Larasort - For Eloquent ORM\n\n**Larasort** is useful when using the Eloquent ORM.\n\n## Basic Usage\n\nFirst, your Model must use the `AutoSortable` Trait.\n\nThen 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`.\n\nNote: the first column of the array `$sortables` will be the column used by default for the SQL `ORDER BY`.\n\nExample:\n\n```php\n\u003c?php\n\nuse SDamian\\Larasort\\AutoSortable;\n\nclass Customer extends Model\n{\n    use AutoSortable;\n    \n    /**\n     * The attributes that are sortable.\n     */\n    private array $sortables = [\n        'id', // \"id\" column will be the default column for the ORDER BY.\n        'first_name', \n        'email',\n        'created_at',\n    ];\n}\n```\n\nYou can override the column used by default for `ORDER BY` with this static method:\n\nNote: the advantage of using the `setDefaultSortable` method is that even if in the URL there are no `?orderby={column}\u0026order={direction}`, the icon will still appear in the link for the default column.\n\n```php\n\u003c?php\n\nuse SDamian\\Larasort\\Larasort;\n\nLarasort::setDefaultSortable('email') // \"email\" column will be the default column for the ORDER BY.\n```\n\nIf by default (when in the URL there is no `?orderby={column}`), you don't want to apply `ORDER BY` in the SQL query:\n\n```php\n\u003c?php\n\nuse SDamian\\Larasort\\AutoSortable;\n\nclass Customer extends Model\n{\n    use AutoSortable;\n    \n    /**\n     * The attributes that are sortable.\n     */\n    private array $sortables = [\n        null, // Will be null by default (by default there will be no ORDER BY).\n        'id',\n        'first_name', \n        'email',\n        'created_at',\n    ];\n}\n```\n\nThen with eloquent, you can use the `-\u003eautosort()` magic method:\n\n```php\n\u003c?php\n\nuse App\\Models\\Customer;\n\nclass CustomerController extends Controller\n{\n    public function index()\n    {\n        $customers = Customer::whereNotNull('confirmed_at')\n            -\u003eautosort() // Automate ORDER BY and its direction.\n            -\u003epaginate();\n\n        return view('customer.index', [\n            'customers' =\u003e $customers,\n        ]);\n    }\n}\n\n```\n\nAnd in the view you can do this in the `thead` of a `table` for example:\n\nNote: You must put the `with-larasort` CSS class on the `table` tag which encloses the blade directive (important: `with-larasort` CSS class on the `table` tag by example. And you also need the `thead` tag).\n\n```html\n\u003ctable class=\"with-larasort\"\u003e\n    \u003cthead\u003e\n        \u003ctr\u003e\n            \u003cth\u003e\n                @sortableLink('first_name', 'First name')\n            \u003c/th\u003e\n            \u003cth\u003e\n                @sortableLink('email', 'Email')\n            \u003c/th\u003e\n            \u003cth\u003e\n                @sortableLink('created_at', 'Register on')\n            \u003c/th\u003e\n        \u003c/tr\u003e\n    \u003c/thead\u003e\n    \u003ctbody\u003e\n        @foreach ($rows as $row)\n\n        @endforeach\n    \u003c/tbody\u003e\n\u003c/table\u003e\n```\n\nNote: 1st parameter is the `column` in database, 2nd parameter is the `title` (`label`). The 2nd parameter is optional. If you don't specify pass, the label will be generated automatically based on the column name.\n\nIf you need to keep more control inside a `th`, for more control you can replace `@sortableLink` by `@sortableHref` and `@sortableIcon`.\n\nExample:\n\n```html\n\u003cth\u003e\n    \u003ca @sortableHref('email')\u003e\n        Email\n        @sortableIcon('email')\n    \u003c/a\u003e\n\u003c/th\u003e\n```\n\n## Aliasing\n\nIf for some columns you do not want to specify the table in prefix, you must use the `$sortablesAs` property.\n\nIn a concrete case, aliases are especially useful when you make an SQL query with a join.\n\n### Example with -\u003ejoin()\n\n- Example in the Customer Model:\n\n```php\n\u003c?php\n\nuse SDamian\\Larasort\\AutoSortable;\n\nclass Customer extends Model\n{\n    use AutoSortable;\n    \n    /**\n     * The attributes that are sortable.\n     */\n    private array $sortables = [\n        'id',\n        'first_name', \n        'email',\n        'created_at',\n    ];\n\n    /**\n     * The attributes that are sortable without table in prefix.\n     */\n    private array $sortablesAs = [\n        'article_title', // Here.\n    ];\n}\n```\n\n- Example in a CustomerController:\n\n```php\n\u003c?php\n\nclass CustomerController extends Controller\n{\n    public function index()\n    {\n        $customers = Customer::select([\n                'customers.*',\n                'articles.title AS article_title', // Here.\n            ])\n            -\u003ejoin(\n                'articles',\n                'customers.id', '=', 'articles.customer_id'\n            )\n            -\u003eautosort() // Automate ORDER BY and its direction.\n            -\u003epaginate();\n\n        return view('customer.index', [\n            'customers' =\u003e $customers,\n        ]);\n    }\n}\n```\n\n- Example in View (in blade template):\n\n```html\n@sortableLink('article_title', 'Article Title')\n```\n\n\n## Relationships\n\nWith **Larasort** you can automate the `ORDER BY` of your relations One To One and One To Many.\n\nTo do this, you can use the `autosortWith` method.\n\n### One To One\n\nIn this example, a `user` has created one `article`, and an `article` has been created by a single `user`.\n\nThis therefore creates a **One To One** relationship between `users` and `articles`.\n\n- Example in User Model:\n\n```php\n\u003c?php\n\n/**\n * The attributes of its sortable relations.\n */\nprivate array $sortablesRelated = [\n    // Convention: {relationship name}{separator}{column in this relationship table}.\n    'article.title',\n];\n\npublic function article()\n{\n    return $this-\u003ehasOne(Article::class, 'user_id_created_at', 'id');\n}\n```\n\n- Example in UserController:\n\n```php\n\u003c?php\n\n$users = User::autosortWith('article', [\n        'join_type' =\u003e 'join', // Optional - \"leftJoin\" by default.\n        'columns' =\u003e ['id', 'username', 'email', 'role'], // Optional - \"*\" by default.\n        'related_columns' =\u003e ['title AS article_title', 'h1'], // Optional - \"*\" by default.\n    ])\n    -\u003epaginate();\n```\n\n- Example in View (in blade template):\n\n```html\n@sortableLink('article.title', 'Article Title')\n```\n\nNote: for the 1st argument of `@sortableLink`, use the same convention as in the `$sortablesRelated` property of the Model.\n\n### One To Many\n\nIn this example, a `user` has created multiple `articles`, and an `article` has been created by a single `user`.\n\nThis therefore creates a **One To Many** relationship between `users` and `articles` (several articles per user, and only one user per article).\n\n- Example in User Model:\n\n```php\n\u003c?php\n\n/**\n * The attributes of its sortable relations.\n */\nprivate array $sortablesRelated = [\n    // Convention: {relationship name}{separator}{column in this relationship table}.\n    'articles.title',\n];\n\npublic function articles()\n{\n    return $this-\u003ehasMany(Article::class, 'user_id_created_at', 'id');\n}\n```\n\n- Example in UserController:\n\n```php\n\u003c?php\n\n$users = User::autosortWith('articles', [\n        'join_type' =\u003e 'join', // Optional - \"leftJoin\" by default.\n        'columns' =\u003e ['id', 'username', 'email', 'role'], // Optional - \"*\" by default.\n        'related_columns' =\u003e ['title AS article_title', 'h1'], // Optional - \"*\" by default.\n    ])\n    -\u003epaginate();\n```\n\n- Example in View (in blade template):\n\n```html\n@sortableLink('articles.title', 'Article Title')\n```\n\nNote: for the 1st argument of `@sortableLink`, use the same convention as in the `$sortablesRelated` property of the Model.\n\n### Belongs To\n\nWhether for a **One To One** or **One To Many** relationship, you must put the **belongsTo** method in the Article Model.\n\n- Example in Article Model:\n\n```php\n\u003c?php\n\nprivate array $sortablesRelated = [\n    // Convention: {relationship name}{separator}{column in this relationship table}.\n    'user.email',\n];\n\npublic function user()\n{\n    return $this-\u003ebelongsTo(User::class, 'user_id_created_at', 'id');\n}\n```\n\n- Example in ArticleController:\n\n```php\n\u003c?php\n\n$articles = Article::autosortWith('user', [\n        'join_type' =\u003e 'join', // Optional - \"leftJoin\" by default.\n        'columns' =\u003e ['id', 'slug', 'h1', 'updated_at'], // Optional - \"*\" by default.\n        'related_columns' =\u003e ['email AS user_email', 'first_name'], // Optional - \"*\" by default.\n    ])\n    -\u003epaginate();\n```\n\n- Example in View (in blade template):\n\n```html\n@sortableLink('user.email', 'User Email')\n```\n\nNote: for the 1st argument of `@sortableLink`, use the same convention as in the `$sortablesRelated` property of the Model.\n\n### Relationships - Conventions\n\n#### Model $sortablesRelated property\n\nFor the columns you put in the `$sortablesRelated` property, the convention is: `{relationship name}{separator}{column in this relationship table}`.\n\nLarasort will use `{relationship name}` to do the `ORDER BY` on its table.\n\nBy default the separator is a period. If you wish, you can change it in the config with `relation_column_separator`.\n\n#### -\u003eautosortWith() method options\n\nTo do the join, you must specify the name of the relation in the first parameter of `-\u003eautosortWith()`.\n\nInside, you must pass the name of your relation (the name of the relation method that you put in your Model). Larasort will use this name to do the `join`.\n\n##### Options:\n\nPS:\nIf at the first parameter of `-\u003eautosortWith()` you put a relation name different from what you had put at `{relationship name}` of the property `$sortablesRelated`, the `ORDER BY` simply won't happen on the relationship.\n\n- \"`join_type`\" (optional):\n\nTo make another join than the default (the one specified in the config), you can specify the `join_type` option.\n\n- \"`columns`\" (optional):\n\nIf you want to specify the columns to `SELECT` for your Model, you can specify the `columns` option.\n\nYou can put either an array or a string. Example with an array: `['id', 'email', 'username']`. Example with a string: `'id, email, username'`.\n\nBy default the `SELECT` will be done on all the columns.\n\n- \"`related_columns`\" (optional):\n\nIf you want to specify which columns to `SELECT` for your Model's relationship, you can specify the `related_columns` option.\n\nYou can put either an array or a string. Example with an array: `['title AS article_title', 'content']`. Example with a string: `'title AS article_title, content'`.\n\nBy default the `SELECT` will be done on all the columns.\n\n\n## For a Column, Specify its Table\n\nWith **Larasort** you can for columns, specify their table (this is useful when you make a SQL query with join).\n\nBy default, Larasort will do the `ORDER BY` on the table where the `AutoSortable` trait is included.\n\nLet's take an example where in an SQL query you want to retrieve articles (from a `articles` table) and categories (from a `categories` table), and that for these 2 tables you want to retrieve the `id` column. But you want to do `ORDER BY id` on the `categories` table instead of the `articles` table.\n\n### You can solve this problem with these 2 solutions\n\n#### Solution 1 - With $sortablesToTables property\n\nThe `$sortablesToTables` property can optionally be put in the Model:\n\n```php\n\u003c?php\n\nuse SDamian\\Larasort\\AutoSortable;\n\nclass Article extends Model\n{\n    use AutoSortable;\n    \n    /**\n     * The attributes that are sortable.\n     */\n    private array $sortables = [\n        'id',\n        'title',\n        'updated_at',\n    ];\n\n    /**\n     * The sortable attributes to which their table is specified.\n     */\n    private array $sortablesToTables = [\n        'id' =\u003e 'categories.id', // Here.\n    ];\n}\n```\n\n#### Solution 2 - With Larasort::setSortablesToTables(array $sortablesToTables)\n\nThe `Larasort::setSortablesToTables(array $sortablesToTables)` method can optionally be put just before the SQL query where you will use `-\u003eautosort()` (in the Controller or in the Model, for example).\n\nExample in a ArticleController:\n\n```php\n\u003c?php\n\nuse SDamian\\Larasort\\Larasort;\n\nclass ArticleController extends Controller\n{\n    public function index()\n    {\n        Larasort::setSortablesToTables(['id' =\u003e 'categories.id']); // Here.\n\n        // Here your SQL query with -\u003eautosort()\n        // Then the rest of the code...\n    }\n}\n```\n\nIf the `$sortablesToTables` property and the `Larasort::setSortablesToTables(array $sortablesToTables)` method are used at the same time for the same column, the `Larasort::setSortablesToTables(array $sortablesToTables)` method will override the `$sortablesToTables` property.\n\nUsing either of these solutions, the result of the SQL query will be: ```ORDER BY `categories`.`id` ASC``` instead of ```ORDER BY `articles`.`id` ASC ```.\n\n## Put \"desc\" or \"asc\" by Default For Some Columns\n\nIt is possible for some columns, that the order (the direction of the `ORDER BY`) to want it to be by default (or on the 1st click on its link) at `desc` instead of `asc`.\n\nThis can optionally be put just before the SQL query where you will use `-\u003eautosort()` (in the Controller or in the Model, for example).\n\nExample in a InvoiceController:\n\n```php\n\u003c?php\n\nuse SDamian\\Larasort\\Larasort;\n\nclass InvoiceController extends Controller\n{\n    public function index()\n    {\n        Larasort::setSortablesDefaultOrder([\n            'desc' =\u003e ['id', 'created_at', 'price_with_vat'], // Here.\n        ]);\n\n        // Here your SQL query with -\u003eautosort()\n        // Then the rest of the code...\n    }\n}\n```\n\n### If you change \"default_order\" in \"config/larasort.php\" file - Put \"asc\" by default for some columns\n\nIn the `config/larasort.php` config file, you can change the value of `default_order` (which defaults to `asc`).\n\nIf you do this: it is possible for some columns, than the order of wanting it to be at `asc` instead of `desc`.\n\nExample in a InvoiceController:\n\n```php\n\u003c?php\n\nuse SDamian\\Larasort\\Larasort;\n\nclass InvoiceController extends Controller\n{\n    public function index()\n    {\n        Larasort::setSortablesDefaultOrder([\n            'asc' =\u003e ['customer_email', 'customer_first_name'], // Here.\n        ]);\n\n        // Here your SQL query with -\u003eautosort()\n        // Then the rest of the code...\n    }\n}\n```\n\n## Clear Larasort static methods\n\nIf you need to, you can clear (reset) the static methods of Larasort:\n\n```php\n\u003c?php\n\nLarasort::clearDefaultSortable();\n\nLarasort::clearSortablesToTables();\n\nLarasort::clearSortablesDefaultOrder();\n```\n\n## Larasort - API Doc\n\n### Model properties\n\n| Type  | Property           | Description |\n| ----- | ------------------ | ----------- |\n| array | $sortables         | Define columns that are sortable. |\n| array | $sortablesAs       | Define alias columns that are sortable. |\n| array | $sortablesToTables | For column(s), specify its table. |\n\n### Larasort class\n\nFor `SDamian\\Larasort\\Larasort` class:\n\n| Return type | Method                                                   | Description |\n| ----------- | -------------------------------------------------------- | ----------- |\n| void        | ::setDefaultSortable(string $defaultSortable)            | Change the default column (for the SQL `ORDER BY`). |\n| void        | ::clearDefaultSortable()                                 | Clear \"setDefaultSortable\" method. |\n| void        | ::setSortablesToTables(array $sortablesToTables)         | For column(s), specify its table. |\n| void        | ::clearSortablesToTables()                               | Clear \"setSortablesToTables\" method. |\n| void        | ::setSortablesDefaultOrder(array $sortablesDefaultOrder) | Assign default order (\"desc\" or \"asc\") for some columns. |\n| void        | ::clearSortablesDefaultOrder()                           | Clear \"setSortablesDefaultOrder\" method. |\n\n### AutoSortable trait\n\nFor `SDamian\\Larasort\\AutoSortable` trait:\n\n| Return type | Method                        | Description                                      |\n| ----------- | ----------------------------- | ------------------------------------------------ |\n| Builder     | scopeAutosort(Builder $query) | scope to generate the `ORDER BY` of the SQL query. |\n\n### Blade directives\n\n| Return type | Directive                                    | Description                                     | Return example |\n| ----------- | -------------------------------------------- | ----------------------------------------------- |--- |\n| string      | @sortableUrl(string $column)                | Returns the URL of a column.            | `http://www.website.com/utilisateurs?orderby=email\u0026order=asc` |\n| string      | @sortableHref(string $column)                | Returns the href (with its URL in it) of a column.            | `href='http://www.website.com/utilisateurs?orderby=email\u0026order=asc'` |\n| string      | @sortableIcon(string $column)                | Returns the icon (image) of a column, in the correct order. | `\u003cspan class=\"larasort-icon-n-1\"\u003e\u003c/span\u003e` |\n| string      | @sortableLink(string $column, string $label) | Return link of a column = href + label + icon.              | `\u003ca href=\"http://www.website.com/utilisateurs?orderby=email\u0026amp;order=asc\"\u003eEmail\u003cspan class=\"larasort-icon-n-1\"\u003e\u003c/span\u003e\u003c/a\u003e` |\n\n\n# LarasortManual - For Without Eloquent ORM\n\n**Larasort** is useful when you weren't using the Eloquent ORM.\n\nIf you want to do a manual SQL query (or if you want to do a file listing), an alternative exists: **LarasortManual**.\n\n## LarasortManual - Basic Usage\n\nWith **LarasortManual**, the `setSortables(array $sortables)` method is useful to define the columns allowed to be sorted in the `ORDER BY`.\n\nSimple example:\n\n```php\n\u003c?php\n\nuse SDamian\\Larasort\\Manual\\LarasortManual;\n\nclass CustomerController extends Controller\n{\n    public function index()\n    {\n        $larasortMan = new LarasortManual();\n        $larasortMan-\u003esetSortables(['id', 'first_name', 'email', 'created_at']); // Here.\n        $resultLarasortMan = $larasortMan-\u003eget();\n\n        $customers = DB::select('\n            SELECT *\n            FROM customers\n            ORDER BY '.$resultLarasortMan['order_by'].' '.$resultLarasortMan['order'].'\n        ');\n\n        return view('customer.index', [\n            'customers' =\u003e $customers,\n            'larasortManAttrs' =\u003e $resultLarasortMan['attrs'],\n        ]);\n    }\n}\n```\n\nAnd in the view you can do this in the `thead` of a `table` for example:\n\n```html\n\u003ctable class=\"with-larasort\"\u003e\n    \u003cthead\u003e\n        \u003ctr\u003e\n            \u003cth\u003e\n                \u003ca {!! $larasortManAttrs['first_name']['href'] !!}\u003e\n                    First name\n                    {!! $larasortManAttrs['first_name']['icon'] !!}\n                \u003c/a\u003e\n            \u003c/th\u003e\n            \u003cth\u003e\n                \u003ca {!! $larasortManAttrs['email']['href'] !!}\u003e\n                    Email\n                    {!! $larasortManAttrs['email']['icon'] !!}\n                \u003c/a\u003e\n            \u003c/th\u003e\n            \u003cth\u003e\n                \u003ca {!! $larasortManAttrs['created_at']['href'] !!}\u003e\n                    Register on\n                    {!! $larasortManAttrs['created_at']['icon'] !!}\n                \u003c/a\u003e\n            \u003c/th\u003e\n            \u003cth\u003eActions\u003c/th\u003e\n        \u003c/tr\u003e\n    \u003c/thead\u003e\n    \u003ctbody\u003e\n        @foreach ($rows as $row)\n\n        @endforeach\n    \u003c/tbody\u003e\n\u003c/table\u003e\n```\n\nNote: if you wish, you can also have access to `$larasortManAttrs['column_name']['url']`.\n\n## LarasortManual - For a Column, Specify its Table\n\nWith **LarasortManual** also you can for columns, specify their table (this is useful when you make a SQL query with join).\n\nUnlike **Larasort** which makes the SQL query on the table where the `AutoSortable` trait is included, by default, **LarasortManual** will do the `ORDER BY column` without specifying a table in prefix.\n\nSo, when you join multiple tables, if you `SELECT` the same column name on several tables, you can end up with an error like: *\"Integrity constraint violation: 1052 Column '{colomn}' in order clause is ambiguous\"*.\n\nLet's take an example where in an SQL query you want to retrieve articles (from a `articles` table) and categories (from a `categories` table), and that for these 2 tables you want to retrieve the `id` column. And you want to do `ORDER  id` on the `categories` table.\n\nYou can use the `$larasortMan-\u003esetSortablesToTables(array $sortablesToTables)` method to achieve this.\n\nExample:\n\n```php\n\u003c?php\n\nuse SDamian\\Larasort\\Manual\\LarasortManual;\n\nclass ArticleController extends Controller\n{\n    public function index()\n    {\n        $larasortMan = new LarasortManual();\n        $larasortMan-\u003esetSortables(['id', 'title', 'created_at']);\n        $larasortMan-\u003esetSortablesToTables(['id' =\u003e 'categories.id']); // Here.\n        $resultLarasortMan = $larasortMan-\u003eget();\n\n        // Here your SQL query with $resultLarasortMan['order_by'] and $resultLarasortMan['order']\n        // Then the rest of the code...\n    }\n}\n```\n\n`$resultLarasortMan['order_by']` will generate the SQL query ```ORDER BY `categories`.`id` ASC``` instead of ```ORDER BY `id` ASC```.\n\n## LarasortManual - Put \"desc\" or \"asc\" by Default for Some Columns\n\nWith **LarasortManual** also you can for some columns, have the order (the direction of ORDER BY) default (or on the 1st click on its link) to `desc` instead of `asc`.\n\nYou can do this with the `$larasortMan-\u003esetSortablesDefaultOrder(array $sortablesDefaultOrder)` method.\n\nExample:\n\n```php\n\u003c?php\n\nuse SDamian\\Larasort\\Manual\\LarasortManual;\n\nclass InvoiceController extends Controller\n{\n    public function index()\n    {\n        $larasortMan = new LarasortManual();\n        $larasortMan-\u003esetSortables(['id', 'ref', 'customer_email', 'created_at', 'price_with_vat']);\n        $larasortMan-\u003esetSortablesDefaultOrder([\n            'desc' =\u003e ['id', 'created_at', 'price_with_vat'], // Here.\n        ]);\n        $resultLarasortMan = $larasortMan-\u003eget();\n\n        // Here your SQL query with $resultLarasortMan['order_by'] and $resultLarasortMan['order']\n        // Then the rest of the code...\n    }\n}\n```\n\n### If you change \"default_order\" at \"config/larasort.php\" file - Put \"asc\" by default for some columns\n\nYou can do this in exactly the same way as with Larasort. By doing something like this:\n\n```php\n\u003c?php\n\n$larasortMan-\u003esetSortablesDefaultOrder([\n    'asc' =\u003e ['customer_email', 'customer_first_name'], // Here.\n]);\n```\n\n## LarasortManual - API Doc\n\n### LarasortManual class\n\nFor `SDamian\\Larasort\\Manual\\LarasortManual` class:\n\n| Return type | Method                                                 | Description |\n| ----------- | ------------------------------------------------------ | ----------- |\n| void        | setSortables(array $sortables)                         | To specify sortable columns. |\n| void        | setSortablesToTables(array $sortablesToTables)         | For column(s), specify its table. |\n| void        | setSortablesDefaultOrder(array $sortablesDefaultOrder) | Assign default order (\"desc\" or \"asc\") for some columns. |\n| array       | get() | Return the result of LarasortManual instance. |\n\n\n## Support\n\nIf you discover a **bug** or a **security vulnerability**, please send a message to Stephen. Thank you.\n\nAll bugs and all security vulnerabilities will be promptly addressed.\n\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](./LICENSE) file for more details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs-damian%2Flarasort","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fs-damian%2Flarasort","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs-damian%2Flarasort/lists"}