I have three different tables:
- Users: ID, name, etc...
- Teams: ID, creator_id, etc...
- Team_Action: ID, team_id, etc...
- Permissions: ID, name, etc...
Team_Action has a FK (action_id) with Perimissions which I need the name value of that table. Team_Action also has a FK (user_id) with Users which I need the name value of the users table.
At the time, I can only access ID's and not names.
I was wondering if there is any way to achieve the following by only using Laravel's models:
@foreach($team->teamAction as $tmdata)
<tr>
<td>
$team->teamMember->name
</td>
<td>
$team->teamAction->name
</td>
</tr>
@endforeach
my Team model:
//Infinite users belong to a team
public function teamMembers() {
return $this->hasMany(TeamsMembers::class,'team_id','id');
}
//Infinite actions per team
public function teamAction(){
return $this->hasMany(TeamsAction::class);
}
TeamAction
public function team(){
return $this->belongsTo(Team::class);
}
Permission
public function teamAction(){
return $this->belongsTo(TeamsAction::class,'action_id');}
TeamsMembers
public function team() {
return $this->belongsTo(Team::class,'team_id','id');
}
public function teamMember(){
return $this->hasMany(User::class,'id','user_id');
}
My controllers code:
$teamID = $request->route('id');
$team = Team::where('id',$teamID)->get()->first();
foreach ($team->teamAction as $tta) {
$allTeamActions = Permission::where('id', $tta->action_id)->get();
}
foreach ($team->teamMembers as $ttm){
$allTeamMembers = User::where('id', $ttm->user_id)->get();
}
I am aiming for something like this
$allTeamActions = Permission::where('id', $team->teamAction[0]->action_id->name)->get();
EDIT:
Let me put on some visuals to make it more clear:
Is there a way to retrieve through eloquent models the name values which are FK's of those models to the model I am using? 1
The scenario is the following: Each member belongs to a Team. (Table Teams: ID, creator_id) The team has multiple members in another table named team_members (user_id, team_id). A team has multiple actions stored in another table named team_actions which store FK values coming from another table named permissions (team_id,action_id_user_id). A user table stores all the actual information of the users (aka their names which I'd like to use)
I want to create a view table with all that information. Namely, I am trying though the Teams model to access the following information: Team users (their names coming from users table, the ID of the user is stored in team_members and team_actions)
Team user actions (the name of the action coming from the permission table, the ID of the action is stored in team_actions)