Reputation: 4282
I'm using Laravel 5.4
, Laravel Roles
from here and Eloquent
relationships.
I'm trying to retrieve the users of a company along with their roles.
User::find(1)->roles
gets me the user's roles whose id =1
Company::find(1)->users
gets me all the users that belongs to the company whose id =1
(without the roles of users).
Company::find(1)->users->roles
returns an error Property [roles] does not exist on this collection instance.
class User extends Authenticatable
{
use HasRoleAndPermission;
public function company()
{
return $this->belongsTo('App\Company');
}
public function user()
{
return $this->belongsTo(User::class);
}
}
trait HasRoleAndPermission
{
public function roles()
{
return $this->belongsToMany(config('roles.models.role'));
}
}
class Company extends Model
{
public function users() {
return $this->hasMany('App\User');
}
}
Upvotes: 0
Views: 2436
Reputation: 13259
$company = Company::with('users.roles')->find(1);
Will load all the users, and all the roles for each user.
Update
According to your comment, you don't want the company data. Just users and roles Using eager loading and relationship existence.
$users = User::with('roles')
->whereHas('company' => function($query){
$query->where('name', '=', 'company'); //If you don't have the company ID
})->get();
Without relationship existence
$users = User::where('company_id', 1)->with('roles')->get();
Upvotes: 4
Reputation: 2701
1 company has many users.
1 users has many roles.
You are trying to get the roles of a collection of users (the property only exists for one user) thus, the property doesn't exists for the collection.
If you want to get all the roles of all users in the company, you might try the above code:
$roles = [];
Company::find(1)->users->foreach(function($user) {
$roles = array_merge($roles, $user->roles);
});
--------- edit ---------
For eager loading the roles of users, you must use with
, as suggested by @Ohgodwhy, but I'd refactor a little:
$users = User::with('roles')->where('company_id', $companyId)->get();
Now you have the array of users eager loading their roles. You still can't access directly $users->roles
, you must first get a user
, only then get its roles
.
Upvotes: 1