PHP code example of ardalanamini / laravel-custom-relation

1. Go to this page and download the library: Download ardalanamini/laravel-custom-relation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ardalanamini / laravel-custom-relation example snippets


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

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

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

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

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',
                  collect($models)->map(function ($value) {
                      return $value->getKey();
                  })->values()->unique()->sort()->all()
               );
            }
        );
    }
}

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',
                    collect($models)->map(function ($value) {
                        return $value->getKey();
                    })->values()->unique()->sort()->all()
                );
            }
        );
    }
}