Since you are having a belongsToMany
relationship between the User
and Company
, the User
belongs to more than one Company
. To get users of the companies of a particular User
will not be straight forward. If you are sure that is exactly what you want, then do this:
//inside the User model
public function companies()
{
return $this->belongsToMany('Company');
}
//inside the User model
public function companiesusers()
{
$users= new Illuminate\Database\Eloquent\Collection;
foreach($this->companies as $company)
{
$users = $users->merge($company->users->get());
}
return $users->unique();
}
//inside the Company model
public function users()
{
return $this->belongsToMany('User');
}
Then you can get a user's companiesusers
like so:
User::first()->companiesusers();
myCompany
the relationship you posted? – Jonas Staudenmeir