msafadi/laravel-eloquent-join-with

Selecting only specific columns within the join

Opened this issue · 1 comments

I'm trying to understand how to pass a list of columns to select rather than using all columns in a relationship.

Specific References

The following show function definitions within the Builder.php class where a column list can be provided:

public function get($columns = ['*'])

public function getModels($columns = ['*'])

protected function applyJoinsWith(&$columns = ['*'])

It appears there may be a way to provide a column list to the builder.

Example

In a standard Laravel with clause, you would select only specific relationship columns as:

$query->with(['profile:id,type'])->...

Or in a join you could do (from Laravel's Documentation):

use Illuminate\Support\Facades\DB;
 
$users = DB::table('users')
            ->join('contacts', 'users.id', '=', 'contacts.user_id')
            ->join('orders', 'users.id', '=', 'orders.user_id')
            ->select('users.*', 'contacts.phone', 'orders.price')
            ->get();

Due to the way the joinWith structures the sql query, we need to pass a column list to the above references. Can you please provide some insight on how or whether this is a feature that is available at this time?

As I mentioned in the README documentation:
"Specifying which columns of the relationship you would like to retrieve is not supported yet but might be added in future versions."

Even though you actually still be able to use the select() method when building your query, but it will be ignored for the related model columns.

This is because for now I'm adding all the related table columns with custom aliases in order to be able to identify them later when populating the model relationship.