Download the PHP package waavi/model without Composer
On this page you can find all versions of the php package waavi/model. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package model
Short Description Eloquent models with auto validation on save and update.
License
Informations about the package model
Better Eloquent models for Laravel
WaaviModel is inspired by Ardent and Aware model validators for Eloquent. The following features are provided:
- Allows Laravel to query related models.
- AutoValidate on model save.
- Allows for unique constraint in validation rules to auto ignore the current model when updating
- Flashes input data when a save fails.
Setup
Edit composer.json:
"require": {
"waavi/model": "dev-master"
},
Models must extend the Waavi\Model\WaaviModel class:
<?php
use Waavi\Model\WaaviModel;
class MyModel extends WaaviModel {
...
}
Validation
WaaviModel uses Laravel's Validator class, therefore validation rules, custom messages and custom validation methods are all available.
All of Laravel's validation rules are available, as well as any custom validation rules you've setup. When saving a model, the validation is run automatically. Validation errors may be retrieved through $model->errors(). Usage example:
In case you want to skip validation, you may do so using . You may check that a model is valid before saving using
Rules may be accessed and modified at runtime using:
Custom messages are accessed in the same way:
Querying related models
WaaviModels extend Eloquent's ability to query related models. This is useful when queries must be built dynamically and require you to access values in related models.
Let us explain it through an example. Say you have the following models:
Say you want all Posts made by users named John or Jane that have been tagged as 'News'. This would be hard to do with Eloquent, but with WaaviModel you have an alternative. You may call whereRelated($relationshipName, $column, $operator, $value) to filter the results:
If you want to retrive all posts other than those posted by people named John its just as easy.
How about getting all of the post comments made in articles posted by John?
All relationships supported by Eloquent are supported by WaaviModel, including MorphOne and MorphMany. The list of available filters is:
Closures as well as deep relationships as supported, as you saw in the previous examples. However, subqueries are not yet supported, and $column, $operator and $value must be specified except when using a Closure.
Calls to whereRelated methods are internally compiled to whereIn, whereNotIn or whereNull (when no records satisfy the constraint). To do so, every call queries the database to see which records comply with the specified constraint. This has several drawbacks:
- Most common databases allow for a limited number of elements (around 10.000) in whereIn clauses. Laravel has the same limitation when using eager loading, which is done in the same way.
- Each whereRelated call queries the database separately, so heavy use will impact performance.