P粉4712073022023-07-27 13:42:45
根据您的解释,我按照以下方式进行操作:
表格 *************
project_user 表:
Schema::create('project_user', function (Blueprint $table) { $table->unsignedBigInteger('project_id'); $table->unsignedBigInteger('user_id'); });
permission_role 表:
Schema::create('permission_role', function (Blueprint $table) { $table->unsignedBigInteger('permission_id'); $table->unsignedBigInteger('role_id'); });
role_user 表:
Schema::create('role_user', function (Blueprint $table) { $table->unsignedBigInteger('role_id'); $table->unsignedBigInteger('user_id'); });
Models *************
用户模型:
/** * Team Relationship * * @return BelongsToMany */ public function teams(): BelongsToMany { return $this->belongsToMany(Team::class); } /** * Project Relationship * * @return BelongsToMany */ public function projects(): BelongsToMany { return $this->belongsToMany(Project::class); } /** * Role Relationship * * @return BelongsToMany */ public function roles(): BelongsToMany { return $this->belongsToMany(Role::class, 'role_user'); }
团队模型:
/** * User Relationship * * @return BelongsToMany */ public function users(): BelongsToMany { return $this->belongsToMany(User::class); } /** * Project Relationship * * @return HasMany */ public function projects(): HasMany { return $this->hasMany(Project::class); }
项目模型:
/** * Team Relation * * @return BelongsTo */ public function team(): BelongsTo { return $this->belongsTo(Team::class); } /** * User Relation * * @return BelongsToMany */ public function users(): BelongsToMany { return $this->belongsToMany(User::class); }
角色模型:
/** * Permission Relation * * @return BelongsToMany */ public function permissions(): BelongsToMany { return $this->belongsToMany(Permission::class, 'permission_role'); } /** * User Relation * * @return BelongsToMany */ public function users(): BelongsToMany { return $this->belongsToMany(User::class,'role_user'); }
权限模型:
/** * Role Relation * * @return BelongsToMany */ public function roles(): BelongsToMany { return $this->belongsToMany(Role::class, 'permission_role'); }
首先,我们创建我们期望的角色。例如:
角色1:名称=超级管理员,类型=用户
角色2:名称=团队主管,类型=团队
角色3:名称=项目开发者,类型=项目
现在,我们将期望的角色分配给我们的用户。这些角色存储在role_user表中。
然后,根据这些角色,您可以确定每个用户在每个项目和团队中的职责。