P粉4712073022023-07-27 13:42:45
Based on your explanation, I did it as follows:
Table******************
project_user table:
Schema::create('project_user', function (Blueprint $table) { $table->unsignedBigInteger('project_id'); $table->unsignedBigInteger('user_id'); });
permission_role table:
Schema::create('permission_role', function (Blueprint $table) { $table->unsignedBigInteger('permission_id'); $table->unsignedBigInteger('role_id'); });
role_user table:
Schema::create('role_user', function (Blueprint $table) { $table->unsignedBigInteger('role_id'); $table->unsignedBigInteger('user_id'); });
Models *************
User model:
/** * 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'); }
Team Model:
/** * 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); }
Project Model:
/** * 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); }
Role Model:
/** * 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'); }
Permission Model:
/** * Role Relation * * @return BelongsToMany */ public function roles(): BelongsToMany { return $this->belongsToMany(Role::class, 'permission_role'); }
First, we create the roles we expect. For example:
Role 1: Name = Super Administrator, Type = User
Role 2: Name = Team Director, Type = Team
Role 3: Name = Project Development user, type=project
Now we assign the desired role to our user. These roles are stored in the role_user table.
Then, based on these roles, you can determine each user's responsibilities within each project and team.