Laravel 5 Eloquent scope join and select specific columns

bakamike

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.

The Alpha

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to select specific columns in laravel eloquent

Select in eloquent join laravel

Laravel select specific columns with Eloquent while eager loading

CONCAT columns with Laravel 5 eloquent

Select except some columns in laravel eloquent when i use from "with" in join queries

How to get specific columns in Laravel Eloquent with pagination?

Get Specific Columns Using “With()” Function in Laravel Eloquent

Get Specific Columns in HasOne relationship in Laravel Eloquent

Laravel 5.2 eloquent eager loading select columns

Select specific column in laravel eloquent's with method

Laravel eloquent select data only specific term

laravel 5 - Select all columns without asterix when joining tables with eloquent or query builder

Select specific columns with a condition in Laravel

Laravel Eloquent query scope with join that uses DB::raw

Laravel Eloquent - Select all columns plus a subquery using Eloquent only

Laravel 5 - Override Get or Select from Eloquent

How to join three table by laravel eloquent model Laravel 5

laravel eloquent union, join 2 columns in the same table

How can I change "join with select statement results to" laravel eloquent?

Get specific columns using “only()” function in Laravel Eloquent

Laravel 5: How to do a join query on a pivot table using Eloquent?

How to perform a join containing a sub query on an Eloquent Relationship in laravel 5

Laravel 5 - Join 3 tables in eloquent with the combination result

Select specific columns using EF in MVC 5

How to select columns from joined tables: laravel eloquent

Is there a way to select columns using eloquent one to many relationship in laravel 5.2?

Laravel Eloquent ORM - Is it possible set default select columns list?

Laravel eloquent join in ajax

Laravel Eloquent join vs with