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表中。
然後,根據這些角色,您可以確定每個使用者在每個專案和團隊中的職責。