thytanium/ eloquent-positionable
使用位置属性对Eloquent模型进行排序
v0.2.0
2023-01-18 17:41 UTC
Requires
- php: ^8.0
- illuminate/database: >=6.0
- illuminate/support: >=6.0
Requires (Dev)
- laravel/laravel: >=6.0
- mockery/mockery: ^1.4
- phpunit/phpunit: 9.5.27
README
使用属性对Eloquent模型进行排序。
这是一个零配置包,提供了一个特性来处理Eloquent模型的定位/排序功能。
兼容性
PHP >= 8.0 和 Laravel >= 8.0。
安装
此包可以通过composer安装。
无需担心服务提供者。此包不公开服务提供者。
composer require thytanium/eloquent-positionable
用法
- 将特性
Thytanium\EloquentPositionable\Positionable添加到您的模型中。 - 可选地,指定列名。
- 如果没有提供列名,将使用列
position对模型进行定位。
- 如果没有提供列名,将使用列
use Illuminate\Database\Eloquent\Model; use Thytanium\EloquentPositionable\Positionable; class MyModel extends Model { use Positionable; // optional parameters protected $positionable = [ 'column' => 'column_name', // column used for positioning 'start' => 5, // starting position ]; }
创建新模型将自动分配下一个可用的位置。
$myModel = new MyModel(); $myModel->save(); $myModel->getPosition(); // by default will return 1, or the custom starting position
API
排序
moveTo(int $position)
将模型移动到指定的位置。
$model->moveTo(5); // moves model to position 5
moveToStart()
将模型移动到开始位置。
$model->moveToStart();
moveToEnd()
将模型移动到末尾位置。
$model->moveToEnd();
moveUp(?int $places = 1)
将模型向上移动指定数量的位置。默认 1。
$model->moveUp(); // moves the model 1 place up $model->moveUp(5); // moves the model 5 places up
moveDown(?int $places = 1)
将模型向下移动指定数量的位置。默认 1。
$model->moveDown(); // moves the model 1 place down $model->moveDown(5); // moves the model 5 places down
moveStep(int $step)
通过位置变化(步长)移动模型。正值向上移动模型。负值向下移动模型。
$model->moveStep(-1); // moves the model 1 place up $model->moveStep(1); // moves the model 1 place down
setNewOrder(array $ids, int $startPosition = 1, ?string $primaryKeyColumn = null)
为指定的id设置新的顺序。
Model::setNewOrder([1, 2, 3]); // sets sequencial order on models with ids 1, 2 and 3 starting from position 1 Model::setNewOrder([1, 2, 3], 5); // sets sequencial order on models with ids 1, 2 and 3 starting from position 5
查询
ordered($order = 'asc'): \Illuminate\Database\Eloquent\Builder
查询作用域,按模型的位置排序,即获取排序后的列表。
MyModel::ordered()->get(); // get all models in ascending order MyModel::ordered('desc')->get(); // get all models in descending order
position(int $position): \Illuminate\Database\Eloquent\Builder
查询作用域,搜索位置等于$position的模型。
MyModel::position(1)->first(); // get the model at the start
positionBetween(array $between): \Illuminate\Database\Eloquent\Builder
查询作用域,搜索位置在两个值之间的模型。
MyModel::positionBetween([1, 9])->get(); // get models between positions 1 and 9 MyModel::positionBetween([1, 9])->ordered()->get(); // get models in ordered fashion between positions 1 and 9
交换
swapPositions(\Illuminate\Database\Eloquent\Model|int $target)
与另一个模型或位置交换位置。
$model1->swapPositions($model2); // swap position with another model instance $model1->swapPositions(2); // swap position with whichever model holds position 2
组
如果您的模型有分组字段,如user_id,可以通过指定分组列名来处理
use Illuminate\Database\Eloquent\Model; use Thytanium\EloquentPositionable\Positionable; class MyModel extends Model { use Positionable; // optional parameters protected $positionable = [ 'column' => 'column_name', // column used for positioning 'start' => 5, // starting position 'groups' = ['user_id'], // columns for grouping ]; }
示例
注意事项
虽然这个特性确保同一个位置不会由多个模型使用,但不要在位置列上添加UNIQUE索引。这将在未来的版本中解决。
测试
该包包含使用PHPUnit设置的单元/集成测试。
composer test
变更日志
有关最近更改的更多信息,请参阅CHANGELOG.md。
贡献
贡献使开源社区成为一个如此神奇的学习、灵感和创造的地方。您所做的任何贡献都将受到高度赞赏。
如果您有改进建议,请fork存储库并创建一个pull request。您也可以简单地打开一个带有“增强”标签的问题。别忘了给项目点个赞!再次感谢!
- fork项目
- 创建您的功能分支(
git checkout -b feature/AmazingFeature) - 提交您的更改(
git commit -m 'Add some AmazingFeature') - 推送到分支(
git push origin feature/AmazingFeature) - 打开pull request
替代方案
许可证
MIT许可证(MIT)。有关更多信息,请参阅许可证文件。