Enables simple sort for your Eloquent models
composer require monurakkaya/laravel-sortable
$fooOne = Foo::create(['title' => 'Foo1', 'sort_order' => 1]);
$fooTwo = Foo::create(['title' => 'Foo2', 'sort_order' => 4]);
$fooThree = Foo::create(['title' => 'Foo3', 'sort_order' => 3]);
$records = Foo::get();
-Foo1
-Foo3
-Foo2
Your model should use Monurakkaya\Traits\Sortable trait to enable auto sort;
use Monurakkaya\Sortable\Traits;
class Foo extends Model {
use Sortable;
}
and that's all.
To change the column name to be sorted just override the getSortableColumn
method on your model
class Foo extends Model {
use Sortable;
protected static function getSortableColumn()
{
return 'created_at';
}
}
Default is sort_order
To change the sorting direction just override the getSortableDirection
method on your model
class Foo extends Model {
use Sortable;
protected static function getSortableDirection()
{
return 'desc';
}
}
Default is asc
** You do not have to add new column, you can use existing columns like created_at, id
etc to make your model able to auto-sort results.
// This will generate an unsigned big integer column named `sort_order`
// which equivalent to $table->unsignedBigInteger('sort_order');
Schema::create('table', function (Blueprint $table) {
...
$table->sortableColumn();
});
Schema::table('table', function (Blueprint $table) {
$table->dropSortableColumn();
});
// on create
$table->sortableColumn('my_column_name');
// on drop
$table->dropSortableColumn('my_column_name');
Using schema helper is optional. You can go on with your own definitions.
To run the tests, execute the following from the command line, while in the project root directory:
./vendor/bin/phpunit