Download the PHP package johnnyfreeman/laravel-custom-relation without Composer
On this page you can find all versions of the php package johnnyfreeman/laravel-custom-relation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download johnnyfreeman/laravel-custom-relation
More information about johnnyfreeman/laravel-custom-relation
Files in johnnyfreeman/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/johnnyfreeman/laravel-custom-relation
Informations about the package laravel-custom-relation
This repo was a bit of a science experiment to find a solution to this issue. I now believe a better to handle custom relations is to create a new class possibly extending one of the existing classes.
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:
User
Role
Permission
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 Permission
s for a User
, or all the User
s 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.