tranghaviet/laravel-custom-relation

当库存关系不足时,使用自定义关系。

v0.0.6 2021-08-29 17:52 UTC

This package is auto-updated.

Last update: 2024-09-29 05:43:16 UTC


README

当库存关系不足时,使用自定义关系。

当...

  • 没有现成的关系适合。 (例如:BelongsToManyThrough)

安装

推荐的安装方式是使用composer

composer require tranghaviet/laravel-custom-relation

示例

假设我们有3个模型

  • User
  • Role
  • Permission

假设UserRole存在多对多关系,而RolePermission也存在多对多关系。

所以它们的模型可能看起来像这样。(故意简化了。)

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);
    }
}

如果你想要获取某个User的所有Permission或者所有具有特定PermissionUser,怎么办?Laravel中没有现成的关联来描述这种情况。我们需要的是BelongsToManyThrough,但现成的Laravel中没有这样的东西。

解决方案

首先,确保你的模型使用了HasCustomRelations特质。然后,定义自定义关系如下。

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));
            }
        );
    }
}

你现在可以像处理普通关系一样进行所有操作,而无需先查询中间关系。