Eloquent Select behaving oddly when querying a belongsToMany relationship

Viewed 22

I am trying to retrieve and paginate the related User models of a Group model.

The response should only contain first_name, but there seems to be two issues causing additional fields to get added.

  1. Because an 'appends' is defined it seems to be bringing in those fields as well even though I have a select statement.

  2. When I use the paginate() function it seems to omit my select statement altogether and brings in all fields. When I use get() instead, I at least get first_name + the appends.

Questions:

How do you disable appends for a single query?

Why isn't the select statement working when paginating results of the belongsToMany relationship? Why would all fields be returned instead of the ones passed to select()?


I simplified/changed the code to show what I'm trying to do.

Group Model

    protected $fillable = [
        'name',
    ]


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

User Model

    protected $fillable = [
        'first_name',
        '... many more fields....'
    ]
    public $appends = [
        'profile_pic'
    ];

Controller:

$users = $group->users();

// returns all fillable and appends fields instead of just first_name
$paginated = $users->select('first_name')->paginate(10);


Thank you!

0 Answers
Related