Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

biha's avatar
Level 1

Project with roles

As nobody answered my other questions I assumed that they were kinda hard to work with, and I'm here just to make a simple question:

I have administrators, developers and costumers as roles for users. Developers can create projects, meaning that he is administrator on his project, but he can add other people to be administrator or just to watch.

How can I make a role system for both the website and the projects part and tell the different if the user is administrator on a project or on the website?

Thanks in advance.

0 likes
14 replies
RachidLaasri's avatar

To separate a SITEadministrator from other users :

  • Add a field to your users table named 'role' for example, the site administrator will have 'admin' as a role and others will just have 'user'.

To separate a PROJECT administrator from costumers :

  • You will need to create a new table named 'users_projects' or anything you want wit fields :
    • id // Identifier
    • user_id // The user id
    • project_id // the project id
    • role // can be either 'admin' or 'costumer'
mstnorris's avatar

@biha, the answers provided in the other questions will suffice and do what you want. However, I would point out that you should be careful of who you refer to as Administrators as it will very quickly become confusing. I already showed you how to use Middleware and a field to your users table for admins etc.

Let's say for arguments sake from now on we refer to Administrators as either "ProjectAdministrator" or "SystemAdministrator".

So, we still have Users (and the roles of Developer, Project Administrator, and System Administrator) am I right?

User Model

public function roles() {
    return $this->belongsToMany('App\Role'); // a User can have many Roles
}

public function projects() {
    return $this->belongsToMany('App\Project'); // a User can be an Administrator on many Projects
}

Role Model

public function users() {
    return $this->belongsToMany('App\User'); // a Role can belong to many Users
}

Project Model

public function developer() {
    return $this->hasOne('App\User'); // this is the Project's owner so to speak
}

public function administrators() {
    return $this->belongsToMany('App\User'); // these are the delegated Administrators
}

This should get you going.

biha's avatar
Level 1

@RachidLaasri - I didn't quite understand what you wanted to say. You said to create a field and assign 'admin' or 'user'? It would be really complicated, I would be duplicating the roles every single time I add a new user. What if I need to change that?

@mstnorris - Let's assume I take this approach of saying Project Administrator, and System Administrator. Imagine that I have more than one project, he creates one, he is the administrator. Other developer creates another one, this one is the administrator and adds the first one too as editor. How can I tell the difference of the first one in different projects by his roles? - And a project doesn't "have" a developer, if the developer creates the project he is an administrator.

I have a simple role system using many to many relationship, and also I have projects... The thing is that I want to reuse the roles system, knowing that the roles names are equal, at least for most of them, and for that I created a pivot table called project_user, with project_id, user_id and role_id.

And now I have those problems:

1º - In order to add a new user to a project I have to specify the role_id, something like that: `$project->users()->attach($user, ['role_id' => $role->id]) it works, but I can't attach more than one role to that user in a project. I read the documentation and it says that I can pass an array, but I got some errors:

$project->users()->attach([
    $user, ['role_id' => 1],
    $user, ['role_id' => 2]
]);

I hard-coded the id of the roles just as an example, and I got this:

Illuminate\Database\QueryException with message 'SQLSTATE[HY000]: General error : 25 bind or column index out of range (SQL: insert into "project_user" ("created_ at", "project_id", "updated_at", "user_id")

As you an see in this small part of the error, there's no role_id.

2º - As I mentioned above, it works if I attach a user with one role, but the problem is that I can't retrieve that role as an object, only its id, isn't there a way to associate the role_id with the id of role's table, just like the pivot table does with the project_id and user_id? If not, what can I do, I really want to avoid doing another query just to retrieve the role.

In this one I though of using a mutator maybe.

3º - This one is related to onDelete. The user_role has an onDelete method that says that when the role is deleted, delete the record related to it on the pivot table. It works for user_role, but it doesn't work for the project_user, even if the role has been deleted, the project_user's record still there with the role_id.

mstnorris's avatar

@biha You said:

And a project doesn't "have" a developer, if the developer creates the project he is an administrator.

Well in that case you don't need a specific relation as such:

Project Model

public function developer() {
    return $this->hasOne('App\User'); // this is the Project's owner so to speak
}

But how can you tell who own's the project? As in who started it?

I think at this stage until you really work out what it is you're trying to achieve, we're never going to get anywhere as it is such an open ended question. I don't think you know what you want yet.

biha's avatar
Level 1

@mstnorris

I think at this stage until you really work out what it is you're trying to achieve, we're never going to get anywhere as it is such an open ended question. I don't think you know what you want yet.

The other question I accepted because the question were different and the problems appeared after.. and I said that the project doesn't have a developer was because I didn't understand your code, you repeated the belongs to many but changed only the name of the method. Let me simplify my question: How would you add a new user to a project and associate a role for him in your case?

mstnorris's avatar

@biha you mentioned previously that there is no distinction between roles within a project. You said that there are no Developers, just Administrators.

So if we're talking about a standard project_user pivot table then just by being included in the table, you are a part of the project. Meaning, that you are an Administrator.

biha's avatar
Level 1

@mstnorris - You misunderstood what I said: When a developer creates a project he becomes administrator of that project, not "developer", as "developer" is on the website itself, not on his project, that is why I said that there's no "developer" on the project, and also because you posted a code public function developer() and public function administrators(), and I didn't understand that part. The thing is: In a project will be administrator, editor and costumer, and regular users.

biha's avatar
Level 1

@mstnorris - In order to clean everything up, forget that I have the issue above and try to answer this:

I want to create a role system for the website that has a panel, the roles are: administrator, developer and costumer, and of course, regular user. A developer on his panel can create a project, and on that project has other roles, he will become administrator and can add more users to it as: administrator, editor, costumer or regular users. How can I do that?

mstnorris's avatar

@biha well the example I showed you above is just that; an example. You could have called it what you like.

Can you give me a full specification of what you're trying to achieve, with your roles and how you see them working together. Then, I will try and give you something to get you started.

Cool, you just added it ;)

Update

I just started writing a fairly lengthy reply, but I have one question that I think you should think about:

Regarding Roles, they only exist with respect to a Project, do they not?

biha's avatar
Level 1

@mstnorris - The user as a developer on the website can he hired, and he creates projects as a case or to work along with his costumer. On the website panel he will be aware of how many projects he has and so on, and on the project panel he will be able to change and add more people to it.

biha's avatar
Level 1

@mstnorris - I'm still waiting for you help, but I'm searching too. I read about polymorphic relationship but they doesn't seem help in this case.

WayneLuke's avatar

I would think that your project model would have a owner_id in its record. Then you would use the role of developer to limit who can make projects. Then you know who the owner is. You can't determine the owner by their role alone. The project would belongTo a user but a user can hasMany projects.

Please or to participate in this conversation.