Download the PHP package s-damian/larasort without Composer
On this page you can find all versions of the php package s-damian/larasort. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package larasort
Column sorting for Laravel - Sortable - Sort by
Larasort : Column sorting for Laravel - Sort easily
Introduction - Larasort package
This package allows you to automate the of your SQL queries, as well as to automate the generation of sortable links.
This Open Source library allows to make 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 🚀
Simple example with Larasort
-
Example in Model:
-
Example in Controller:
- Example in View (in blade template):
Example rendering of a link in a table:
Author
This package is developed by Stephen Damian
Requirements
- PHP 8.0 || 8.1 || 8.2 || 8.3
- Laravel 8 || 9 || 10 || 11
Summary
- Installation
- Customization with "vendor:publish"
- Larasort - For Eloquent ORM
- Basic usage
- Aliasing
- Example with ->join()
- Relationships
- One To One
- One To Many
- Belongs To
- Relationships - Conventions
- For a column, specify its table
- Put "desc" or "asc" by default for some columns
- Clear Larasort static methods
- Larasort - API Doc
- Model properties
- Larasort class
- AutoSortable trait
- Blade directives
- LarasortManual - For without Eloquent ORM
- LarasortManual - Basic usage
- LarasortManual - For a column, specify its table
- LarasortManual - Put "desc" or "asc" by default for some columns
- LarasortManual - API Doc
- LarasortManual class
- Support
- License
Installation
Installation via Composer:
Customization with "vendor:publish"
Custom Config and Lang and CSS
After installing the package, you have to run the command:
The command will generate these files:
-
(you must include this CSS in your website)
You can of course customize these files.
"vendor:publish" with "--tag" argument
Publish only file:
Publish only file:
Publish only file:
Larasort - For Eloquent ORM
Larasort is useful when using the Eloquent ORM.
Basic usage
First, your Model must use the Trait.
Then it is necessary that in your Model you declare . This property is useful for defining the columns (columns in your DB table) allowed to be sorted in the .
PS: the 1st column of the array will be the column used by default for the SQL .
Example:
You can override the column used by default for with this static method:
PS: the advantage of using the method is that even if in the URL there are no , the icon will appear the same in the link of the default column.
If by default (when in the URL there is no ), you don't want to put to the SQL query:
Then with eloquent, you can use the magic method:
And in the view you can do this in the of a for example:
PS: You must put the CSS class on a HTML tag which encloses the blade directive (on the or tag by example).
PS: 1st parameter is the in database, 2nd parameter is the (). The 2nd parameter is optional. If you don't specify pass, the label will be generated automatically based on the column name.
If you need to keep more control inside a th, as an equivalent you can replace by and . Example:
Aliasing
If for some columns you do not want to specify the table in prefix, you must use the property.
In a concrete case, aliases are especially useful when you make an SQL query with a join.
Example with ->join()
-
Example in a Customer Model:
-
Example in a CustomerController:
- Example in View (in blade template):
Relationships
With Larasort you can automate the of your relations One To One and One To Many.
To do this, you can use the method.
One To One
In this example, a has created one , and an has been created by a single .
This therefore makes a One To One relationship between and .
-
Example in User Model:
-
Example in UserController:
- Example in View (in blade template):
PS: for the 1st argument of , use the same convention as in the property of the Model.
One To Many
In this example, a has created multiple , and an has been created by a single .
This therefore makes a One To Many relationship between and (several articles per user, and only one user per article).
-
Example in User Model:
-
Example in UserController:
- Example in View (in blade template):
PS: for the 1st argument of , use the same convention as in the property of the Model.
Belongs To
Whether for a One To One or One To Many relationship, you must put the belongsTo method in the Article Model.
-
Example in Article Model:
-
Example in ArticleController:
- Example in View (in blade template):
PS: for the 1st argument of , use the same convention as in the property of the Model.
Relationships - Conventions
Model $sortablesRelated property
For the columns you put in the in the property, the onventions is:
Larasort will use to do the on its table.
By default the separator is a period. If you wish, you can change it in the config with .
->autosortWith() method options
To do the join, you must specify the name of the relation in the first parameter of .
Inside, 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 .
Options:
PS: If at the first parameter of you put a relation name different from what you had put at of the property , the simply won't happen on the relationship.
- "" (optional):
To make another joint than default (the one specified in the config), you can specify the option.
- "" (optional):
If you want to specify the columns to for your Model, you can specify the option.
You can put either an array or a string. Example with an array: Example with a string:
By default the will be done on all the columns.
- "" (optional):
If you want to specify which columns to for your Model's relationship, you can specify the option.
You can put either an array or a string. Example with an array: Example with a string:
By default the will be done on all the columns.
For a column, specify its table
With Larasort you can for columns, specify their table (this is useful when you make a SQL query with join).
By default, Larasort will do the on the table where the trait is included.
Let's take an example where in an SQL query you want to retrieve articles (from a table) and categories (from a table), and that for these 2 tables you want to retrieve the column. But you want to do on the table instead of on the table.
You can solve this problem with these 2 solutions
Solution 1 - With $sortablesToTables property
The property can optionally be put in the Model:
Solution 2 - With Larasort::setSortablesToTables(array $sortablesToTables)
The method can optionally be put just before the SQL query where you will use (in the Controller or in the Model, for example).
Example in a ArticleController:
If 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)
metho method will override the $sortablesToTables
property.
With these 2 solutions, the result of the SQL queries will be: ORDER BY `categories`.`id` ASC
instead of ORDER BY `articles`.`id` ASC
Put "desc" or "asc" by default for some columns
It 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
.
This can optionally be put just before the SQL query where you will use ->autosort()
(in the Controller or in the Model, for example).
Example in a InvoiceController:
If you change "default_order" at "config/larasort.php" file - Put "asc" by default for some columns
In the config/larasort.php
config file, you can change the value of default_order
(which defaults to asc
).
If you do this: it is possible for some columns, than the order of wanting it to be at asc
instead of desc
.
Example in a InvoiceController:
Clear Larasort static methods
If you need to, you can clear (reset) the static methods of Larasort:
Larasort - API Doc
Model properties
Type | Property | Description |
---|---|---|
array | $sortables | Define columns that are sortable. |
array | $sortablesAs | Define alias columns that are sortable. |
array | $sortablesToTables | For column(s), specify its table. |
Larasort class
For SDamian\Larasort\Larasort
class:
Return type | Method | Description |
---|---|---|
void | ::setDefaultSortable(string $defaultSortable) | Change the default column (for the SQL ORDER BY ). |
void | ::clearDefaultSortable() | Clear "setDefaultSortable" method. |
void | ::setSortablesToTables(array $sortablesToTables) | For column(s), specify its table. |
void | ::clearSortablesToTables() | Clear "setSortablesToTables" method. |
void | ::setSortablesDefaultOrder(array $sortablesDefaultOrder) | Assign default order ("desc" or "asc") for some columns. |
void | ::clearSortablesDefaultOrder() | Clear "setSortablesDefaultOrder" method. |
AutoSortable trait
For SDamian\Larasort\AutoSortable
trait:
Return type | Method | Description |
---|---|---|
Builder | scopeAutosort(Builder $query) | scope to generate the ORDER BY of the SQL query. |
Blade directives
Return type | Directive | Description | Return example |
---|---|---|---|
string | @sortableUrl(string $column) | Returns the URL of a column. | http://www.website.com/utilisateurs?orderby=email&order=asc |
string | @sortableHref(string $column) | Returns the href (with its URL in it) of a column. | href='http://www.website.com/utilisateurs?orderby=email&order=asc' |
string | @sortableIcon(string $column) | Returns the icon (image) of a column, in the correct order. | <span class="larasort-icon-n-1"></span> |
string | @sortableLink(string $column, string $label) | Return link of a column = href + label + icon. | <a href="http://www.website.com/utilisateurs?orderby=email&order=asc">Email<span class="larasort-icon-n-1"></span></a> |
LarasortManual - For without Eloquent ORM
Larasort is useful when you weren't using the Eloquent ORM.
If you want to do a manual SQL query (or if you want to do a file listing), an alternative exists: LarasortManual
LarasortManual - Basic usage
With LarasortManual, the setSortables(array $sortables)
method is useful to define the columns allowed to be sorted in the ORDER BY
. Simple example:
And in the view you can do this in the thead of a table for example:
<thead class="with-larasort">
<tr>
<th>
<a {!! $larasortManAttrs['first_name']['href'] !!}>
First name
{!! $larasortManAttrs['first_name']['icon'] !!}
</a>
</th>
<th>
<a {!! $larasortManAttrs['email']['href'] !!}>
Email
{!! $larasortManAttrs['email']['icon'] !!}
</a>
</th>
<th>
<a {!! $larasortManAttrs['created_at']['href'] !!}>
Register on
{!! $larasortManAttrs['created_at']['icon'] !!}
</a>
</th>
<th>Actions</th>
</tr>
</thead>
PS: if you wish, you can also have access to $larasortManAttrs['column_name']['url']
LarasortManual - For a column, specify its table
With LarasortManual also you can for columns, specify their table (this is useful when you make a SQL query with join).
Unlike 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.
So, 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".
Let'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 BY id
on the categories
table.
You can do this with the $larasortMan->setSortablesToTables(array $sortablesToTables)
method. Example:
$resultLarasortMan['order_by']
will generate the SQL query ORDER BY `categories`.`id` ASC
instead of ORDER BY `id` ASC
LarasortManual - Put "desc" or "asc" by default for some columns
With 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
.
You can do this with the $larasortMan->setSortablesDefaultOrder(array $sortablesDefaultOrder)
method. Example:
If you change "default_order" at "config/larasort.php" file - Put "asc" by default for some columns
You can do this in exactly the same way as with Larasort. By doing something like this:
LarasortManual - API Doc
LarasortManual class
For SDamian\Larasort\Manual\LarasortManual
class:
Return type | Method | Description |
---|---|---|
void | setSortables(array $sortables) | To specify sortable columns. |
void | setSortablesToTables(array $sortablesToTables) | For column(s), specify its table. |
void | setSortablesDefaultOrder(array $sortablesDefaultOrder) | Assign default order ("desc" or "asc") for some columns. |
array | get() | Return the result of LarasortManual instance. |
Support
Bugs and security Vulnerabilities
If you discover a bug or a security vulnerability, please send a message to Stephen. Thank you.
All bugs and all security vulnerabilities will be promptly addressed.
License
This project is an Open Source package under the MIT license. See the LICENSE file for details.