timwassenburg / default-model-order
Adds a trait to make your models sortable
This package is auto-updated.
Last update: 2024-12-08 12:54:00 UTC
README
Features
This simple package adds a trait to your project that can be used to sort collections of a model by default by a specified column. As a result, the model looks more clean and readable.
Installation
Install the package with composer.
composer require timwassenburg/default-model-order
Usage
Once you have installed the package, you can import the DefaultOrder
trait into your model to set the default order. Then, you can assign the desired column to the $orderBy
variable to order by that column. Additionally, you may optionally set the $orderDirection
variable to specify the direction of the order, with the default value being asc
.
use TimWassenburg\DefaultModelOrder\DefaultOrder; class User extends Model { use DefaultOrder; protected static string $orderBy = 'name'; protected static string $orderDirection = 'desc'; }
Publish config (optional)
Suppose you wish to modify the default column name or direction. In that case, you may publish the configuration file to make the necessary adjustments. To do so, run the following command:
php artisan vendor:publish --provider="TimWassenburg\DefaultModelOrder\DefaultModelOrderServiceProvider" --tag="config"
Contributing
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
The MIT License (MIT). Please see License File for more information.