2017 © Pedro Peláez
 

library laravel-custom-relation

A custom relation for when stock relations aren't enough.

image

johnnyfreeman/laravel-custom-relation

A custom relation for when stock relations aren't enough.

  • Monday, January 16, 2017
  • by johnnyfreeman
  • Repository
  • 6 Watchers
  • 30 Stars
  • 3,071 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 8 Forks
  • 3 Open issues
  • 4 Versions
  • 18 % Grown

The README.md

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., (*1)

Laravel Custom Relation

A custom relation for when stock relations aren't enough., (*2)

Use this if...

  • None of the stock Relations fit the bill. (BelongsToManyThrough, etc)

Installation

The recommended way to install is with composer:, (*3)

composer require johnnyfreeman/laravel-custom-relation

Example

Let's say we have 3 models:, (*4)

  • 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., (*5)

So their models might look something like this. (I kept them brief on purpose.), (*6)

class User
{
    public function roles() {
        return $this->belongsToMany(Role::class);
    }
}

```php class Role { public function users() { return $this->belongsToMany(User::class); }, (*7)

public function permissions() {
    return $this->belongsToMany(Permission::class);
}

}, (*8)

```php
class Permission
{
    public function roles() {
        return $this->belongsToMany(Role::class);
    }
}

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., (*9)

Solution

First, make sure your models are using the HasCustomRelations trait. Then, define custom relations like this., (*10)

use LaravelCustomRelation\HasCustomRelations;

class User
{
    use HasCustomRelations;

    /**
     * Get the related permissions
     *
     * @return Illuminate\Database\Eloquent\Relations\Relation
     */
    public function permissions()
    {
        return $this->custom(
            Permission::class,

            // add constraints
            function ($relation) {
                $relation->getQuery()
                    // join the pivot table for permission and roles
                    ->join('permission_role', 'permission_role.permission_id', '=', 'permissions.id')
                    // join the pivot table for users and roles
                    ->join('role_user', 'role_user.role_id', '=', 'permission_role.role_id')
                    // for this user
                    ->where('role_user.user_id', $this->id);
            },

            // add eager constraints
            function ($relation, $models) {
                $relation->getQuery()->whereIn('role_user.user_id', $relation->getKeys($models));
            }
        );
    }
}
use LaravelCustomRelation\HasCustomRelations;

class Permission
{
    use HasCustomRelations;

    /**
     * Get the related users
     *
     * @return Illuminate\Database\Eloquent\Relations\Relation
     */
    public function users()
    {
        return $this->custom(
            User::class,

            // constraints
            function ($relation) {
                $relation->getQuery()
                    // join the pivot table for users and roles
                    ->join('role_user', 'role_user.user_id', '=', 'users.id')
                    // join the pivot table for permission and roles
                    ->join('permission_role', 'permission_role.role_id', '=', 'role_user.role_id')
                    // for this permission
                    ->where('permission_role.permission_id', $this->id);
            },

            // eager constraints
            function ($relation, $models) {
                $relation->getQuery()->whereIn('permission_role.permission_id', $relation->getKeys($models));
            }
        );
    }
}

You could now do all the normal stuff for relations without having to query in-between relations first., (*11)

The Versions

16/01 2017

dev-master

9999999-dev https://github.com/johnnyfreeman/laravel-custom-relation

A custom relation for when stock relations aren't enough.

  Sources   Download

MIT

The Requires

 

laravel eloquent relationship relation

04/09 2016

0.0.3

0.0.3.0 https://github.com/johnnyfreeman/laravel-custom-relation

A custom relation for when stock relations aren't enough.

  Sources   Download

MIT

The Requires

 

laravel eloquent relationship relation

31/08 2016

0.0.2

0.0.2.0 https://github.com/johnnyfreeman/laravel-custom-relation

A custom relation for when stock relations aren't enough.

  Sources   Download

MIT

The Requires

 

laravel eloquent relationship relation

30/08 2016

0.0.1

0.0.1.0 https://github.com/johnnyfreeman/laravel-custom-relation

A custom relation for when stock relations aren't enough.

  Sources   Download

MIT

The Requires

 

laravel eloquent relationship relation