james.xue/laravel-sortable
Composer 安装命令:
composer require james.xue/laravel-sortable
包简介
Sortable behaviour for eloquent models
README 文档
README
This package provides a trait that adds sortable behaviour to an Eloquent model.
The value of the order column of a new record of a model is determined by the maximum value of the order column of all records of that model + 1.
Installation
This package can be installed through Composer.
composer require james.xue/laravel-sortable
Usage
To add sortable behaviour to your model you must:
-
Use the trait
James\Sortable\SortableTrait;;Use the trait
James\Sortable\Sortable; -
Optionally specify which column will be used as the order column. The default is
sort_field.
Example
use James\Sortable\SortableTrait; use James\Sortable\Sortable; class MyModel extends Eloquent implements Sortable { use SortableTrait; public $sortable = [ 'sort_field' => 'view', 'sort_when_creating' => true, ]; ... }
If you don't set a value $sortable['sort_field'] the package will assume that your order column name will be named sort_field.
If you don't set a value $sortable['sort_when_creating'] the package will automatically assign the highest order number to a new model;
Assuming that the db-table for MyModel is empty:
$myModel = new MyModel(); $myModel->save(); // sort_field for this record will be set to 1 $myModel = new MyModel(); $myModel->save(); // sort_field for this record will be set to 2 $myModel = new MyModel(); $myModel->save(); // sort_field for this record will be set to 3
You can also move a model:
$myModel = new MyModel(); $myModel->where('id', $id)->first()->move('up'); // up、down、top、end
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 10.2k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-11