I have a model Role which belongs to many Users.
Class Role {
public $fillable = ["name"];
public function users()
{
return $this->belongsToMany('App/Models/User')->select(['user_id']);
}
}
When I retrieve users using with query in Role. I want It would return only user_ids array
Role::with("users")->get();
it should return the following output
[
{
"name": "Role1",
"users" : [1,2,3]
},
{
"name": "Role2",
"users" : [1,2,3]
}
]
Currently it gives following output
[
{
"name": "Role1",
"users" : [
{
user_id : 1
},
{
user_id : 2
},
{
user_id : 3
}
},
{
"name": "Role2",
"users" : [
{
user_id : 1
},
{
user_id : 2
},
{
user_id : 3
}
]
}
]
->lists('user_id')
instead of->all()
. – Jeemusu Aug 19 '15 at 8:09