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 } ] } ]
或者直接获取中间表的role_id: