Download the PHP package tranghaviet/laravel-custom-relation without Composer
On this page you can find all versions of the php package tranghaviet/laravel-custom-relation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tranghaviet/laravel-custom-relation
More information about tranghaviet/laravel-custom-relation
Files in tranghaviet/laravel-custom-relation
Package laravel-custom-relation
Short Description A custom relation for when stock relations aren't enough.
License MIT
Homepage https://github.com/tranghaviet/laravel-custom-relation
Informations about the package laravel-custom-relation
Laravel Custom Relation
A custom relation for when stock relations aren't enough.
Use this if...
- None of the stock Relations fit the bill. (BelongsToManyThrough, etc)
Installation
The recommended way to install is with composer:
Example
Let's say we have 3 models:
UserRolePermission
Let's also say User has a many-to-many relation with Role, and Role has a many-to-many relation with Permission.
So their models might look something like this. (I kept them brief on purpose.)
What if you wanted to get all the Permissions for a User, or all the Users with a particular Permission? There no stock Relation in Laravel to descibe this. What we need is a BelongsToManyThrough but no such thing exists in stock Laravel.
Solution
First, make sure your models are using the HasCustomRelations trait. Then, define custom relations like this.
You could now do all the normal stuff for relations without having to query in-between relations first.