yusufalper / laravel-order
#todo: description will be here
Fund package maintenance!
yusufalper
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
This Laravel package provides a handy trait called HasOrder designed to simplify the management of ordered records in your Eloquent models. By applying this trait to your models, you introduce an 'order' attribute, allowing you to effortlessly handle the ordering of records within a given model.
Installation
You can install the package via composer:
composer require yusufalper/laravel-order
Usage
-
Apply Trait: Simply apply the HasOrder trait to your Eloquent model. Ensure that your model's migration includes a column (integer and nullable) specified in model as '$orderAttrName' (see example in below) to support the ordering functionality.
-
Optional Configuration: Define $orderUnificationAttributes (public array) within your model to fine-tune the ordering behavior according to your application's specific requirements. For example if you add 'user_id' attribute to $orderUnificationAttributes, then your ordering will be user_id based ordering.
-
Automatic Handling: The package takes care of automatic order adjustments during record creation, ensuring a seamless and organized ordering process.
-
Effortless Update and Delete: The HasOrder trait also seamlessly handles updates and deletes, maintaining the correct order of records based on your defined criteria.
Example Usage
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' ]; }
If you have multiple traits that each of which has a boot method, Then you should use like this:
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' ]; }
By integrating the HasOrder trait into your Laravel models, you streamline the process of managing ordered records, offering enhanced control and flexibility.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.