demo: http://dcat.weiwait.cn (admin:admin)
composer require weiwait/dcat-orderable
php artisan admin:ide-helper
// 升序
$grid->model()->orderBy('order');
$grid->column('order')->sequencable();
class Model implements \Spatie\EloquentSortable\Sortable
{
use \Weiwait\DcatOrderable\SortableTrait
protected array $sortable = [
'order_column_name' => 'order',
// alias: 'column' => 'order'
'sort_when_creating' => true, // default
// alias: 'sortable' => true
]
}
// 降序
$grid->model()->orderByDesc('order');
$grid->column('order')->sequencable();
class Model implements \Spatie\EloquentSortable\Sortable
{
use \Weiwait\DcatOrderable\SortableTrait
protected array $sortable = [
'order_column_name' => 'order',
// alias: 'column' => 'order'
'sort_when_creating' => true, // default
// alias: 'sortable' => true
'direction' => 'desc' // when using descend method is required
]
}
// 分组排序
// 一般场景:二级分类排序、商品根据分类排序...
$grid->model()->orderByDesc('order');
if (request('parent_id')) {
$grid->column('order')->sequencable();
}
$grid->filter(function ($filter) {
$filter->equal('parent_id')
->select([...$options])
})
class Model implements \Spatie\EloquentSortable\Sortable
{
use \Weiwait\DcatOrderable\SortableTrait
// an attribute of this model
protected $parent_id;
protected array $sortable = [
'order_column_name' => 'order',
// alias: 'column' => 'order'
'sort_when_creating' => true, // default
// alias: 'sortable' => true
'direction' => 'desc' // when using descend method is required
'restiction' => 'parent_id' // where('parent_id', $this->parent_id)
]
}