Laravel - Selecting specific columns on many-to-many relationships
{ return $this->belongsToMany('User')->select(array('id', 'name'));}
use this
The all
method takes in an array of column names as a parameter.
If you look at the source, it takes *
(which means everything) by default.
https://github.com/laravel/framework/blob/4.2/src/Illuminate/Database/Eloquent/Model.php#L624-L629
You can pass in the columns that you needed and it should return the results only with the specified columns.
<?php$groups = Group::with('users')->all(array('first_column', 'third_column'));
Use like this.
<?php class User extends Eloquent { public function groups() { return $this->belongsToMany('Group')->select(array('id', 'name')); } } class Group extends Eloquent { public function users() { return $this->belongsToMany('User')->select(array('id', 'name')); } }?>