yusufalper / laravel-order
#todo: 这里将添加描述
v4.0.0
2024-07-24 18:30 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^10.0
- spatie/laravel-package-tools: ^1.14.0
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^7.8
README
此Laravel包提供了一个名为HasOrder的实用特性,旨在简化Eloquent模型中有序记录的管理。通过将此特性应用于您的模型,您将引入一个'order'属性,允许您轻松处理特定模型内记录的排序。
安装
您可以通过composer安装此包
composer require yusufalper/laravel-order
用法
-
应用特性:简单地将HasOrder特性应用于您的Eloquent模型。确保您的模型迁移包含一个列(整型,可为空),在模型中指定为'$orderAttrName'(见下面的示例)以支持排序功能。
-
可选配置:在您的模型中定义$orderUnificationAttributes(公共数组)以根据您应用程序的特定要求微调排序行为。例如,如果您将'user_id'属性添加到$orderUnificationAttributes中,那么您的排序将是基于'user_id'的排序。
-
自动处理:该包在记录创建期间自动处理订单调整,确保排序过程的无缝和组织。
-
轻松更新和删除:HasOrder特性还无缝处理更新和删除,根据您定义的标准维护记录的正确顺序。
示例用法
use Illuminate\Database\Eloquent\Model; use Alper\LaravelOrder\Traits\HasOrder; class CompanyBranch extends Model { use HasOrder; protected $fillable = [ 'order' 'company_id' ]; public string $orderAttrName = 'order'; public array $orderUnificationAttributes = [ 'company_id' ]; }
如果您有多个特性,每个特性都有一个boot方法,那么您应该这样使用
use Illuminate\Database\Eloquent\Model; use Alper\LaravelOrder\Traits\HasOrder; class CompanyBranch extends Model { use HasOrder { HasOrder::boot as bootHasOrderTrait; } use OtherTrait { OtherTrait::boot as bootOtherTraitTrait; } public static function boot(): void { static::bootHasOrderTrait(); static::bootOtherTraitTrait(); } protected $fillable = [ 'order' 'company_id' ]; public string $orderAttrName = 'order'; public array $orderUnificationAttributes = [ 'company_id' ]; }
通过将HasOrder特性集成到您的Laravel模型中,您可以简化有序记录的管理过程,提供更高级的控制和灵活性。
更新日志
请参阅更新日志以获取有关最近更改的更多信息。
致谢
许可证
MIT许可证(MIT)。请参阅许可证文件以获取更多信息。