Hi
I've been battling the next orm fight. I want to show data from 3 related tables.
user, roles, companies and role_user pivot table.
User model
public function roles() {
return $this
->belongsToMany('App\Role')
->withTimestamps();
}
public function Companies() {
return $this->belongsTo(Companies::class, 'id');
}
Role model
public function users() {
return $this
->belongsToMany('App\User')
->withTimestamps();
}
Companies model
public function User() {
return $this->hasMany(User::class, 'companies_id');
}
UsersController
public function show(Request $request, $id) {
// $user = User::findOrFail($id); <- gets data from user model without companies and roles
// nth attempt..
$user = User::with('roles','companies')->findOrFail($id);
// How do I retrieve/add/join data from companies and roles for specific user?
return view('users.view', compact(['user']));
view
Username: {{ $user->username }}
Email: {{ $user->email }}
Company: {{ $?????->????->company_name }}
Roles: @foreach ($????? as $role)
{{ $role->name . ',' }}
@endforeach
How to correctly add/append the related data and print it out on view?
Thank you