PHPackages                             culturegr/custom-relation - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. culturegr/custom-relation

ActiveLibrary[Database &amp; ORM](/categories/database)

culturegr/custom-relation
=========================

Easy implementation of custom Eloquent relations

v1.4.0(1y ago)1710.3k↓26.4%3[1 issues](https://github.com/culturegr/custom-relation/issues)MITPHPPHP ^8.1|^8.2|^8.3CI passing

Since Dec 11Pushed 1y ago4 watchersCompare

[ Source](https://github.com/culturegr/custom-relation)[ Packagist](https://packagist.org/packages/culturegr/custom-relation)[ Docs](https://github.com/culturegr/custom-relation)[ RSS](/packages/culturegr-custom-relation/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (5)Versions (11)Used By (0)

🏺 Custom Relation
=================

[](#-custom-relation)

[![Latest Version on Packagist](https://camo.githubusercontent.com/f005944a8c4a16250e6291e14d0dccfb6c011f6fe6f7ed2ca2186e721607b93e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f63756c7475726567722f637573746f6d2d72656c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/culturegr/custom-relation)[![Total Downloads](https://camo.githubusercontent.com/e0544ccd05373e991d8a7f1046863946bc6dc0e5f22326dcfe2f8e6112c21be1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f63756c7475726567722f637573746f6d2d72656c6174696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/culturegr/custom-relation)[![Github Actions](https://github.com/culturegr/custom-relation/actions/workflows/run-tests.yml/badge.svg)](https://github.com/culturegr/custom-relation/actions/workflows/run-tests.yml/badge.svg)

This package provides an easy way to implement custom relationships between Eloquent models

Installation
------------

[](#installation)

Via [Composer](https://getcomposer.org):

```
$ composer require culturegr/custom-relation
```

In Laravel 5.5+, the package's service provider should be [auto-discovered](https://laravel.com/docs/5.7/packages#package-discovery), 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`:

```
'providers' => [
    // ...
    CultureGr\CustomRelation\CustomRelationServiceProvider::class,
],
```

Usage
-----

[](#usage)

Suppose we have a Laravel application that implements a simple [ACL](https://en.wikipedia.org/wiki/Access-control_list) (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:

[![Alt text](https://camo.githubusercontent.com/0813bc179eeceeac8ec1e2168383aa47124bd0252d2aaa90b461fdcff037aac3/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f54587578312e706e67 "ACL ER Diagram")](https://camo.githubusercontent.com/0813bc179eeceeac8ec1e2168383aa47124bd0252d2aaa90b461fdcff037aac3/68747470733a2f2f692e737461636b2e696d6775722e636f6d2f54587578312e706e67)

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

[](#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](https://laravel.com/docs/6.x/eloquent-relationships#eager-loading). It can be created by running the `make:relation` Artisan command:

```
$ php artisan make:relation UserPermissionRelation
```

This will generate a new `CustomRelation` class named `UserPermissionRelation` inside `app/Eloquent/CustomRelations` directory with all required boilerplate:

```
