Reputation: 1278
I'm using laravel and sentinel (Sentry) that default using users, groups, and users_groups database table
users
-----------------
id name
1 user
2 user
3 admin
groups
-------------------
id name
1 Users
2 Admins
users_groups
--------------------
user_id group_id
1 1
2 1
3 1
3 2
i define the relation on laravel model and it's work fine when i try to print
$users = Group::find($id)->user;
but how to print just user only?
$users = Group::where(array("name"=>"Users"))->get()->user;
it's still show all data include the admins, i just want to show user only, how to do this?
i know the problem because at users_groups table admin have Users id too..
Please someone help me. It's verry confusing
Upvotes: 1
Views: 129
Reputation: 7003
You can use with
to get the data of specified relation as below :
$users = Group::with('user')->find($id);
This will return data of group with user.
If you just want a data of only user table you have to change the query and rather than using model Group
you have need to query on User
model something like below:
$users = User::where('group_id', $groupId);
http://laravel.com/docs/5.1/eloquent-relationships http://laravel.com/api/5.1/Illuminate/Database/Eloquent/Model.html#method_with
Upvotes: 2