Laravel paginate not working with select certain columns on querybuilder

Viewed 1749

I have user role relation (many to many) defined as follows in Role model:

public function users()
{
    return $this->belongsToMany('App\User');
}

Now I want to show all users in certain role. I execute the following:

$list = Role::find(1)->users()->select('name')->orderBy('name')->paginate(10);

Here $list contain all users that belong to role with id 1 and that is correct. But the problem is: when adding breakpoint on $list it shows that: all user model's attributes are retrieved just like select('name') has no effect. This is a big problem especially when i want to return the $list as json. User password is retrieved even though it's in the $hidden array of User Model. My question is:

why paginate() on querybuilder does not respect select() nor attributes in model's $hidden array?

Note: I know that there is other solutions to achieve what I want. I just want an answer to my question to figure out what is going on?

2 Answers
Related