Download the PHP package cndrsdrmn/eloquent-unique-attributes without Composer
On this page you can find all versions of the php package cndrsdrmn/eloquent-unique-attributes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download cndrsdrmn/eloquent-unique-attributes
More information about cndrsdrmn/eloquent-unique-attributes
Files in cndrsdrmn/eloquent-unique-attributes
Package eloquent-unique-attributes
Short Description A Laravel package to enforce automatic generation of unique attribute values for Eloquent models.
License MIT
Informations about the package eloquent-unique-attributes
Eloquent Unique Attributes
A Laravel package to enforce automatic generation of unique attribute values for Eloquent models, making it easy to maintain uniqueness without manual intervention.
๐ Features
- Automatically generate unique attribute values for Eloquent models.
- Customizable formats (
numerify,lexify, or mixed patterns). - Flexible configuration for prefixes, suffixes, separators, and retries.
- Supports models with soft deletes.
- Fully customizable events for enforcing and notifying unique attribute changes.
๐ Background
Managing unique attributes in Eloquent models often involves tedious logic, especially when combining custom formats and real-time checks. This package provides an automatic, extensible solution. It ensures attributes are unique within the database and adhere to defined configurations.
๐ Installation
Install the package using Composer:
โ๏ธ Configuration
To customize the default behavior, publish the configuration file:
This will create a config/unique_attributes.php file:
Configurable Options:
prefix: A string prepended to the generated value.suffix: A string appended to the generated value.separator: A separator betweenprefix,value, andsuffix.max_retries: Maximum attempts to generate a unique value before increasing length.length: The initial length of the generated unique value.format: Determines the type of value (numerifyfor digits,lexifyfor letters,botifyfor mixed).
๐ Usage
1. Add the Trait to Your Model
Include the HasEnforcesUniqueAttributes trait in any Eloquent model requiring unique attributes:
2. Generate Unique Attributes
When creating the model, the unique values will automatically be enforced:
๐ง Advanced Customization
Event Hooks
This package fires events during the unique value generation process:
eloquent.enforcing: Fired before generating unique attributes. If a listener returns a value, the process of enforcing unique attributes is skipped.eloquent.enforced: After unique attributes are generated.
You can listen to these events in your application to implement custom logic:
Note: If a listener for the
eloquent.enforcingevent returns a value (truthy or falsy), the unique attribute generation process will be skipped for the current model.
๐งช Testing
This package includes PHPUnit tests. To run the tests, use the following command:
๐ค Contributing
We welcome contributions! Follow these steps to contribute:
- Fork the repository.
- Create a new branch (
git checkout -b feature-name). - Commit your changes (
git commit -m "Add new feature"). - Push to the branch (
git push origin feature-name). - Open a Pull Request.
๐ License
This package is open-source software licensed under the MIT license.