@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.