I have an Eloquent model and in the model I am using a scope query to do a join. This all works fine but I am trying to figure out how in the scope query to select only certain columns from the joined table.
Here is the method in my controller.
$accounts = Account::creator()->get();
Here is the scope query in the model
public function scopeCreator($query) {
$query->leftJoin('users','users.id','=','accounts.creator_id');
}
This works but it returns all the columns from accounts and users but I only want all the columns from accounts and only 2 columns from users.
I tried this...
public function scopeCreator($query) {
$query->leftJoin('users','users.id','=','accounts.creator_id')->select(array('id','user_name'));
}
However this returns only those 2 fields from users and ignores the columns from accounts. I know how to do this with 2 different models but that means I have to create a useless model. This scope query functionality is great if I can just get this part figured out.
You may try this:
->select('accounts.*', 'users.id as uid','users.user_name');
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments