Download the PHP package ardalanamini/laravel-custom-relation without Composer
On this page you can find all versions of the php package ardalanamini/laravel-custom-relation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ardalanamini/laravel-custom-relation
More information about ardalanamini/laravel-custom-relation
Files in ardalanamini/laravel-custom-relation
Package laravel-custom-relation
Short Description Create your own custom relation for when stock relations aren't enough
License MIT
Homepage https://github.com/ardalanamini/laravel-custom-relation
Informations about the package laravel-custom-relation
Laravel Custom Relation
Create your own custom relation for when stock relations aren't enough
[TOC]
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.
All versions of laravel-custom-relation with dependencies
laravel/framework Version ^5.0 || ^6.0 || ^7.0 || ^8.0 || ^9.0 || ^10.0