Download the PHP package illuminatech/model-rules without Composer
On this page you can find all versions of the php package illuminatech/model-rules. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download illuminatech/model-rules
More information about illuminatech/model-rules
Files in illuminatech/model-rules
Package model-rules
Short Description Set of the validation rules to check if a model exists or is unique for Laravel
License BSD-3-Clause
Informations about the package model-rules
Laravel Model Validation Rules
This extension provides set of the validation rules to check if a model exists or is unique for Laravel.
For license information check the LICENSE-file.
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the "require" section of your composer.json.
Usage
This extension provides set of the validation rules to check if a model exists or is unique for Laravel. It serves 2 main purposes:
- define a fluent interface for Eloquent model exist/unique validation,
- remove redundant database queries for Eloquent model manipulations.
Assume we have a database storing some items grouped by categories. While creating an HTTP request handler for the new
item saving, we need to check if given category record exists and associate it the new item. There is a well-known
recommendation to use relation methods like associate()
to handle relation instantiation at object level. However,
if we follow it along with standard validation, our program performs redundant SQL query. For example:
To solve this issue you can use Illuminatech\ModelRules\Exists
validation rule. During the validation it "remembers"
the last queried model instance, which can be retrieved using getModel()
method. For example:
You can use Illuminatech\ModelRules\Unique
to setup a validation for unique model attribute. For example:
Customize Database Query
You can pass query builder instance directly to model validation rule. This allows you to specify any custom search conditions or use a relation query. For example:
Note: this extension does not put explicit restriction on the query builder object type - it simply expected to match database query builder notation. Thus, you may create a custom query builder class, which works with special data storage like MongoDB or Redis, and pass its instance as a data source. If its methods signature matches
\Illuminate\Database\Query\Builder
- it should work. Although it is not guaranteed.
Customize error message
You can setup a custom error message for model rules using withMessage()
method. In case a model instance is available
after validation failure, its attributes can be used as a placeholders in the error message using syntax: model_{attribute}
.
For example:
Validate multiple models
Assume we have a database, where items and categories are linked as 'many-to-many'. In such case the request for the item storage will contain a list of category IDs, which should be associated with it. The ordinary request handler for this may look like following:
You can reduce the numbers of database queries for such a scenario using Illuminatech\ModelRules\MultiExist
validation
rule. For example:
Note: as you may have guessed, there is also
Illuminatech\ModelRules\MultiUnique
validation rule, however its real-world implications are quite limited.
Heads up! Remember, that model validation rules are not cumulative, each rule remembers only the last queried model. Thus, it will not serve you well during nested array validation, like in case you want to store multiple items as batch per single HTTP request. It is better to separate validation into multiple steps for such cases. For example:
Working with Form Requests
You can use model rules with Form Request Validation. For example: