Select custom columns from Laravel belongsToMany relation -
im trying select specific attributes on many-to-many relation users
, in one-to-one. using select()
on belongstomany()
seem ignored , i'm still getting user attributes.
class computer extends eloquent { public function users() { return $this->belongstomany("user")->select("email"); } public function admin() { return $this->hasone("user")->select("email"); } } computer::with("users")->get();
is there way of filtering specified columns related entity belongstomany()
?
yes, actually can.
computer::with("users")->get(array('column_name1','column_name2',...));
be careful though if have same column name both tables linked pivot table. in case, need specify table name in dot notation, tablename.columnname
. example if both users , computer has column name id
, need :
computer::with("users")->get(array('users.id','column_name2',...));
Comments
Post a Comment