Download the PHP package culturegr/custom-relation without Composer
On this page you can find all versions of the php package culturegr/custom-relation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download culturegr/custom-relation
More information about culturegr/custom-relation
Files in culturegr/custom-relation
Package custom-relation
Short Description Easy implementation of custom Eloquent relations
License MIT
Homepage https://github.com/culturegr/custom-relation
Informations about the package custom-relation
🏺 Custom Relation
This package provides an easy way to implement custom relationships between Eloquent models
Installation
Via Composer:
In Laravel 5.5+, the package's service provider should be auto-discovered, so you won't need to register it. If for some reason you need to register it manually you can do so by adding it to the providers array in config/app.php
:
Usage
Suppose we have a Laravel application that implements a simple ACL (Access Control List) layer: there are users that are assigned some roles, each of them consists of many permissions. A simplified version of the database structure could be the following:
There is a User
model that has a many-to-many relationship with a Role
model, which in turn has a many-to-many relationship with a Permission
model.
Now suppose that at some point, we need to access all permissions assigned to a specific user. Let's make this possible by creating a CustomRelation
class that will be used to define the relationship between the User
and the Permission
models.
Creating a Custom Relation Class
A CustomRelation class should facilitate all required logic needed to join users
and permissions
tables, as well as to support relationship eager-loading. It can be created by running the make:relation
Artisan command:
This will generate a new CustomRelation
class named UserPermissionRelation
inside app/Eloquent/CustomRelations
directory with all required boilerplate:
Implementing the Custom Relation Class
The UserPermissionRelation
class initializes two properties:
$this->query
provides access to the relatedPermission
model's query builder instance$this->parent
provides access to the parentUser
model
In order to define the users/permissions relationship, the following methods should be implemented:
addConstraints
Sets the base constraints on the relation query. In our example:
addEagerConstraints
Sets the constraints for an eager load of the relation. In our example:
match
Matches the eagerly loaded results to their parents. In our example:
Using the Custom Relation Class
Once the UserPermissionRelation
class has been implemented, it can be used to define a new custom relationship between the User
and the Permission
model via relatesTo
method which is available to the model through HasCustomRelation
trait:
That's it 🔥! Now we can use our new custom permissions
relationship like any usual Eloquent relationship:
Testing
License
Please see the license file for more information.
Credits
- Awesome Laravel/PHP community