Manytomany relationship between three tables

Asked

Viewed 65 times

0

I have the following structure:

Action
-id
-name
-type

Routes
-id
-name
-controllers

User
-id
-name
-email
-password
-usergroup_id

Usergroup
-id
-name
-tenant_id

I want to make a relationship where I can know what actions on a specific route the user can do, and the same thing for the user group, because if the user does not have the action then the actions of his group will be validated.

In this case I would have to create two pivot tables?:

Action_Routes_User
-action_id
-route_id
-user_id

Action_Routes_Usergroup
-action_id
-route_id
-usergroup_id

and spend belongsToMany in everything, right?

i managed to do this relationship, but to get what actions X user has on certain route that problem:

//model User
public function routes()
{
return $this->belongsToMany(Route::class, 'action_route_user', 'user_id', 'route_id');
}

//model Route
public function actionsUser()
{
return $this->belongsToMany(Action::class, 'action_route_user', 'route_id', 'action_id');
}

when I make $user->Routes get all the routes related to this user, ai ok...

but when I take a route from this collection above for example:

$routes = $user->routes;
foreach($routes as $route) {
$routeActions = $route->actionsUser;

// aqui ele está pegando todas as ações independente do usuário ID
// tipo , do usuário que estou logado: Iury pego as rotas que ele tem permissão iury->routes , ai tudo certo
// mas quando pego uma das rotas: $route->actionsUser é como se ele tivesse buscando todas as ações dos usuários nessa tabela pivot action_route_user e não só do iury
}

I believe that a Where user_id is missing when searching in this pivot table, if yes which best way to put?

  • You can try: $route->actionsUser()->where('action_route_user.user_id', $user->id)->get();

  • I put another function inside the User model called actionsUserWhere that does just that but with the id of the route since it is inside the User model, da no mesmo né?

  • 1

    Yeah, but maybe you’re making more calls to the comic book than you’re supposed to, you might as well just log in the user id maybe

  • First, welcome! Second, review your logic. You retrieve a route, and then iterate over it, in search of actions, it will return all relations. It would be interesting to have a direct relationship between the user and the actions

  • Thanks Flavio, well, I created a direct user link for actions, and added a Where, route_id public function actionsWhere($routeId)
 { return $this->belongsToMany(Action::class, 'action_route_user', 'user_id','action_id')->withPivot('route_id') ->wherePivot('route_id', $routeId)->get();} so it worked out! brought all actions related to the id path placed, so I was able to validate in Gate and not let do an action that the user does not have permission, that would be the logic that you spoke?

  • I think you could improve your modeling by ending the action table and going straight through the Routes that would make the system more flexible and less heavy in the matter of bank

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.