简体   繁体   English

如何在laravel中的三个model的pivot中部署关系?

[英]How to deploy relationship in pivot of three model in laravel?

I'm developing a role and permissions based on laravel framework.我正在开发基于 laravel 框架的角色和权限。

I have 3 models:我有 3 个模型:

Weblog
User
Permissions

This is pivot table这是pivot表

user_id , weblog_id , permission_id

Now, a user can have a weblog with permission id 1,2 and another weblog with permission 1,2,3,4现在,一个用户可以拥有一个权限 id 为 1,2 的博客和另一个权限为 1,2,3,4 的博客

How can I deploy relationships?如何部署关系? and how can I check user permissions when managing a weblog.以及如何在管理博客时检查用户权限。 (middleware and...) (中间件和...)

With the fact that Permission are specific to Weblog事实上,权限是特定于博客的

Say the pivot table is called permission_user_weblog说pivot表叫做permission_user_weblog

class User extends Model
{
    public function weblogs()
    {
        return $this->belongsToMany(Weblog::class, 'permission_user_weblog');
    }

    public function permissionsFor(int $weblogId)
    {
        $permissionIds = null;

        $this->weblogs()
            ->where('id', $weblogId)
            ->with('permissions')
            ->get()
            ->each(function($weblog) use(&$permissionIds) {
                $permissionIds = $weblog->permissions->pluck('id');             
            });

        return $permissionIds;
    }
}


class Weblog extends Model
{
    public function users()
    {
        return $this->belongsToMany(User::class, 'permission_user_weblog');
    }

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


class Permission extends Model
{
    public function weblogs()
    {
        return $this->belongsToMany(Weblog::class, 'permission_user_weblog');
    }
}

Then you can check anywhere for whether logged in user has specific permission for a specific weblog然后您可以在任何地方检查登录用户是否具有特定博客的特定权限

public function update(Request $request, $weblogId)
{
    $user = auth()->user();
    $permissions = $user->permissionsFor($weblogId);

    //Check whether the logged in user has permission identified by id 1 or 4 for weblog
    $can = !! $permissions->intersect([1,4])->count();

    //Do rest of processing
}

your Weblog,User,Permission has ManyToMany Relation, its a kind of odd but if you want to have this kind of relation its not a problem.您的博客、用户、权限具有多对多关系,这是一种奇怪的关系,但如果您想拥有这种关系,这不是问题。

just consider each pair a ManyToMany .只需将每一对视为ManyToMany and every one of those can have a hasMany to Pivot (i named it Access) too (based on your needs).并且每个人都可以有一个hasManyPivot (我将其命名为Access)(根据您的需要)。

User model:用户 model:

class User extends Model{

/**
      * retrive weblogs
      *
      * @return BelongsToMany weblogs
      */
     public function weblogs()
     {
         return $this->belongsToMany(App\WebLog::class,'accesses_table')
                        ->withPivot("permission_id")
                        ->using(App\Access::class);
     }

/**
      * retrive permissions
      *
      * @return BelongsToMany permissions
      */
     public function permissions()
     {
         return $this->belongsToMany(App\Permission::class,'accesses_table')
                        ->withPivot("weblog_id")
                        ->using(App\Access::class);
     }

/**
      * retrive access
      *
      * @return hasMany [description]
      */
     public function accesses()
     {
         return $this->hasMany(App\Access::class, "user_id");
     }
}

Weblog model:博客 model:

class Weblog extends Model{

/**
      * retrive users
      *
      * @return BelongsToMany users
      */
     public function users()
     {
         return $this->belongsToMany(App\User::class,'accesses_table')
                        ->withPivot("permission_id")
                        ->using(App\Access::class);
     }

/**
      * retrive permissions
      *
      * @return BelongsToMany permissions
      */
     public function permissions()
     {
         return $this->belongsToMany(App\Permission::class,'accesses_table')
                        ->withPivot("user_id")
                        ->using(App\Access::class);
     }

/**
      * retrive access
      *
      * @return hasMany [description]
      */
     public function accesses()
     {
         return $this->hasMany(App\Access::class, "weblog_id");
     }
}

Permission model:权限 model:

class Permission extends Model{

/**
      * retrieve users
      *
      * @return BelongsToMany users
      */
     public function users()
     {
         return $this->belongsToMany(App\User::class,'accesses_table')
                        ->withPivot("weblog_id")
                        ->using(App\Access::class);
     }

/**
      * retrieve weblogs
      *
      * @return BelongsToMany weblogs
      */
     public function weblogs()
     {
         return $this->belongsToMany(App\Weblog::class,'accesses_table')
                        ->withPivot("user_id")
                        ->using(App\Access::class);
     }

/**
      * retrive access
      *
      * @return hasMany [description]
      */
     public function accesses()
     {
         return $this->hasMany(App\Access::class, "permission_id");
     }
}

and you can have a model for your pivot , which i named it Access :您可以为您的pivot使用model ,我将其命名为Access

Illuminate\Database\Eloquent\Relations\Pivot;

class Access extends Pivot
{

    public $incrementing = true;

public function user()
    {
        return $this->belongsTo(App\User::class);
    }

    public function weblog()
    {
        return $this->belongsTo(App\Weblog::class);
    }

    public function permission()
    {
        return $this->belongsTo(App\Permission::class);
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM