I'm trying to connect companies that are used by specific user role. A user with a specific role can "work" for multiple companies. A company can "employ" multiple users. I have 5 tables (users, role_user, roles, companies and company_user)
Models relations:
App\User:
public function roles()
{
return $this
->belongsToMany('App\Role')
->withTimestamps();
}
public function companies()
{
return $this
->belongsToMany('App\Company')
->withTimestamps();
}
App\Role:
public function users()
{
return $this
->belongsToMany('App\User')
->withTimestamps();
}
App\Companies:
public function users()
{
return $this->belongsToMany('App\User'); // with user_role ??
}
Companies Controller
public function edit(Request $request, $id) {
$company = Company::findOrFail($id);
$users = User::where('role_id',4)->pluck('username')->all(); // no role_id column
$users = User::pluck('username','id')->all(); // returns all users
return view('companies.edit', compact(['company','users']));
}
public function update(Request $request, $id) {
/* TODO */
}
Edit view
{!! Form::select('users[]', $users, null, ['class' => 'form-control', 'multiple' => 'multiple']) !!}
I want to asign users whith a specific user role to these companies. Is there a way to setup the relation or perhaps a scope?
Bonus question :) Is there a simple way to display concatinated value in a dropdown? First name + Last name instead of username?