quadrubo/eloquent-autosort
Composer 安装命令:
composer require quadrubo/eloquent-autosort
包简介
This is my package eloquent-autosort
README 文档
README
This package provides sortable behaviour for Eloquent models using a trait.
The package is based on spatie/eloquent-sortable. See here why this is not merged.
Features
- Automatic sorting on create
- Automatic sorting on update
- Automatic sorting on delete
- Grouping with unlimited Columns
Installation
This package can be installed through Composer.
composer require quadrubo/eloquent-autosort
You can publish the config file with:
php artisan vendor:publish --tag="eloquent-autosort-config"
This is the content of the file that will be published in config/eloquent-autosort.php
return [ /* * Which column will be used as the order column. */ 'order_column_name' => 'order_column', /* * Define if the models should sort when creating. * When true, the package will automatically assign the highest order number to a new model. */ 'sort_when_creating' => true, /** * Define if the models should sort when updating. * When true, the package will automatically update the order of both the old and new group. */ 'sort_when_updating' => true, /** * Define if the models should sort when deleting. * When true, the package will fix the order within the current group when deleting. */ 'sort_when_deleting' => true, /** * Define the columns the model should be grouped by. * You can leave this empty and implement your own solution by overwriting * `buildSortQuery` and `hasChangedGroupAttributes`. */ 'groups' => [], ];
Usage
To add sortable behaviour to your model you must:
- Implement the
Quadrubo\EloquentAutosort\Sortableinterface. - Use the trait
Quadrubo\EloquentAutosort\SortableTrait.
Example
use Quadrubo\EloquentSortable\Sortable; use Quadrubo\EloquentSortable\SortableTrait; class MyModel extends Model implements Sortable { use SortableTrait; public $sortable = [ 'order_column_name' => 'order_column', 'sort_when_creating' => true, 'sort_when_updating' => true, 'sort_when_deleting' => true, 'groups' => [], ]; // ... }
If you don't set a value $sortable['order_column_name'] the package will assume that your order column name will be named order_column.
If you don't set a value $sortable['sort_when_creating'] the package will automatically assign the highest order number to a new model.
If you don't set a value $sortable['sort_when_updating'] the package will automatically repair the order on update.
If you don't set a value $sortable['sort_when_deleting'] the package will automatically repair the order on delete.
If you don't set a value $sortable['groups'] groups will not be used.
Assuming that the db-table for MyModel is empty:
$myModel = new MyModel(); $myModel->save(); // order_column for this record will be set to 1 $myModel = new MyModel(); $myModel->save(); // order_column for this record will be set to 2 $myModel = new MyModel(); $myModel->save(); // order_column for this record will be set to 3 // the trait also provides the ordered query scope. // note that when you use grouping, you should // build the query first for this to be useful. $orderedRecords = MyModel::ordered()->get();
You can set a new order for all the records using the setNewOrder-method
/** * the record for model id 3 will have order_column value 1 * the record for model id 1 will have order_column value 2 * the record for model id 2 will have order_column value 3 */ MyModel::setNewOrder([3,1,2]);
Optionally you can pass the starting order number as the second argument.
/** * the record for model id 3 will have order_column value 11 * the record for model id 1 will have order_column value 12 * the record for model id 2 will have order_column value 13 */ MyModel::setNewOrder([3,1,2], 10);
To sort using a column other than the primary key, use the setNewOrderByCustomColumn-method.
/** * the record for model uuid '7a051131-d387-4276-bfda-e7c376099715' will have order_column value 1 * the record for model uuid '40324562-c7ca-4c69-8018-aff81bff8c95' will have order_column value 2 * the record for model uuid '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1' will have order_column value 3 */ MyModel::setNewOrderByCustomColumn('uuid', [ '7a051131-d387-4276-bfda-e7c376099715', '40324562-c7ca-4c69-8018-aff81bff8c95', '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1' ]);
As with setNewOrder, setNewOrderByCustomColumn will also accept an optional starting order argument.
/** * the record for model uuid '7a051131-d387-4276-bfda-e7c376099715' will have order_column value 10 * the record for model uuid '40324562-c7ca-4c69-8018-aff81bff8c95' will have order_column value 11 * the record for model uuid '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1' will have order_column value 12 */ MyModel::setNewOrderByCustomColumn('uuid', [ '7a051131-d387-4276-bfda-e7c376099715', '40324562-c7ca-4c69-8018-aff81bff8c95', '5dc4d0f4-0c88-43a4-b293-7c7902a3cfd1' ], 10);
You can also move a model up or down with these methods:
$myModel->moveOrderDown(); $myModel->moveOrderUp();
You can also move a model to the first or last position:
$myModel->moveToStart(); $myModel->moveToEnd();
You can also move a model to a specific or the new position:
$myModel->moveToPosition(3); // moves to a position using the value in your order_column attribute $myModel->moveToNewPosition();
You can determine whether an element is first or last in order:
$myModel->isFirstInOrder(); $myModel->isLastInOrder();
You can swap the order of two models:
MyModel::swapOrder($myModel, $anotherModel);
Grouping
If your model/table has one or multiple grouping fields (usually a foreign key): id, user_id, title, order_column and you'd like the package to take it into considerations, you cann add the grouping fields to the $sortable['groups'] array.
public $sortable = [ 'groups' => [ 'user_id', ], ];
This will restrict the calculations to the fields in the array.
Replacing the groups functionality
If you don't like the way the groups functionality is implemented, you can easily make your own.
- Overwrite the
buildSortQuerymethod to change the behaviour of how the query is build. - Overwrite the
hasChangedGroupAttributesmethod to provide a way for the package to know when it should resort.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
Thanks to the Spatie/Eloquent-Sortable Team for the base package functionality!
License
The MIT License (MIT). Please see License File for more information.
quadrubo/eloquent-autosort 适用场景与选型建议
quadrubo/eloquent-autosort 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 135 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 03 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Quadrubo」 「eloquent-autosort」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 quadrubo/eloquent-autosort 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 quadrubo/eloquent-autosort 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 quadrubo/eloquent-autosort 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This is my package filament-model-settings
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
统计信息
- 总下载量: 135
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-10