Download the PHP package alexcrawford/lexorank-sortable without Composer
On this page you can find all versions of the php package alexcrawford/lexorank-sortable. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package lexorank-sortable
Install
Install package through Composer
Version Compatibility
| Laravel | Sortable |
|---|---|
| 4 | 1.2.x (branch laravel4) |
| <=5.3 | 3.2.x |
| 5.4 | 3.4.x |
| 5.5 | 4.2.x |
| 5.7 | 4.7.x |
| 6.0 | 6.0.x |
| 7.x, 8.x | 8.x.x |
| 9.x, 10.x, 11.x, 12.x | 9.x.x |
Sortable Trait
Adds sortable behavior to Eloquent (Laravel) models
Usage
Add position field to your model (see below how to change this name):
Important: Database Collation for Position Column
This package uses LexoRank for position management, which relies on lexicographic (ASCII-based) string sorting. The position values use characters in the range '0' to 'z' (ASCII 48-122).
For SQLite and PostgreSQL:
- ✅ Default text collation works correctly
- No additional configuration needed
For MySQL/MariaDB (CRITICAL):
- ⚠️ You must specify binary collation for the position column
- Default collations like
utf8mb4_unicode_ciwill sort incorrectly
Why this matters:
- Unicode collations (like
utf8mb4_unicode_ci) apply language-specific sorting rules that don't preserve ASCII ordering - Binary collation ensures positions sort in correct lexicographic order (e.g.,
'U'<'V'<'X'<'Z') - Without this, drag-and-drop reordering may produce unexpected results on MySQL/MariaDB
Add \AlexCrawford\Sortable\SortableTrait to your Eloquent model.
if you want to use custom column name for position, set $sortableField:
Now you can move your entities with methods moveBefore($entity) and moveAfter($entity) (you dont need to save
model after that, it has saved already):
Also this trait automatically defines entity position on the create event, so you do not need to add position manually, just create entities as usual:
This entity will be at position entitiesMaximumPosition + 1
To get ordered entities use the sorted scope:
Note : Resorting does not take place after a record is deleted. Gaps in positional values do not affect the ordering of your lists. However, if you prefer to prevent gaps you can reposition your models using the
deletingevent. Something like:You need alexcrawford-sortable >=2.3 to use
->next()
Sortable groups
if you want group entity ordering by field, add to your model
now moving and ordering will be encapsulated by this field.
If you want group entity ordering by many fields, use as an array:
Sortable many to many
Let's assume your database structure is
and you want to order tags for each post
Add position column to the pivot table (you can use any name you want, but position is used by default)
Migration example:
⚠️ Remember: The position column must use binary collation on MySQL/MariaDB for correct sorting.
Add \AlexCrawford\Sortable\BelongsToSortedManyTrait to your Post model and define belongsToSortedMany relation provided by this trait:
Note:
$this->belongsToSortedManyhas different signature then$this->belongsToMany-- the second argument for this method is$orderColumn('position'by default), next arguments are the same
Attaching tags to post with save/sync/attach methods will set proper position
Getting related model is sorted by position
You can reorder tags for given post
Many to many demo: http://sortable5.boxfrommars.ru/posts (code)
You can also use polymorphic many to many relation with sortable behavour by using the MorphsToSortedManyTrait trait and returning $this->morphToSortedMany() from relation method.
By following the Laravel polymorphic many to many table relation your tables should look like
Migration example:
⚠️ Remember: The position column must use binary collation on MySQL/MariaDB for correct sorting.
And your model like
Sortable Controller
Also this package provides \AlexCrawford\Sortable\SortableController, which handle requests to sort entities
Usage
Add the service provider to config/app.php
publish the config:
Add models you need to sort in the config config/sortable.php:
Add route to the sort method of the controller:
Now if you post to this route valid data:
Then entity with \Input::get('id') id will be moved relative by entity with \Input::get('positionEntityId') id.
For example, if request data is:
then the article with id 3 will be moved after the article with id 14.
jQuery UI sortable example
Note: Laravel 5 has csrf middleware enabled by default, so you should setup ajax requests: http://laravel.com/docs/5.0/routing#csrf-protection
Template
Template for many to many ordering
Development
Setup
Build the Docker image:
Running Tests
Code Quality Tools
This project includes modern PHP development tools:
Code Formatting (Laravel Pint):
Static Analysis (PHPStan):
Code Coverage (Xdebug):
Current code coverage:
- Lines: 84.43% (206/244)
- Methods: 76.19% (32/42)
- Classes: 55.56% (5/9)
Running All Tests:
All versions of lexorank-sortable with dependencies
illuminate/support Version ^10.0|^11.0|^12.0
alexcrawford/lexorank-php Version ^2.0