Skip to content

[5.3] Allow loading specific columns while eagerloading - #16327

Merged
taylorotwell merged 3 commits into
laravel:5.3from
themsaid:eagerLoadColumns
Nov 11, 2016
Merged

[5.3] Allow loading specific columns while eagerloading#16327
taylorotwell merged 3 commits into
laravel:5.3from
themsaid:eagerLoadColumns

Conversation

@themsaid

@themsaid themsaid commented Nov 8, 2016

Copy link
Copy Markdown
Member

This allows for:

User::where(...)->with('business:id,name')->get()

Which is equivalent to:

User::where(...)->with([
  'business' => function($q){
      $q->select('id', 'name');
  }
])->get()

@shadoWalker89

Copy link
Copy Markdown
Contributor

How about when we are eager loading nested relationships, and want to select columns on both the direct and distant relations ?

@themsaid

themsaid commented Nov 8, 2016

Copy link
Copy Markdown
Member Author

@shadoWalker89 don't use this syntax :) Just use a closure for more advanced queries.

@taylorotwell
taylorotwell merged commit d3e8543 into laravel:5.3 Nov 11, 2016
@kJamesy

kJamesy commented Nov 16, 2016

Copy link
Copy Markdown

Might be useful to note that you have to include id in the specified columns. Therefore the following doesn't work:
User::where(...)->with('business:name')->get()

@RicardoRamirezR

RicardoRamirezR commented Nov 22, 2016

Copy link
Copy Markdown

@themsaid really cool, is nice and clean.

Would it be possible having:

User::where(...)->with('business:id,name')->get()

To add some spaces:

User::where(...)->with('business: id, name')->get()

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

I think it looks nicer without spaces tbh.

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

Would be out of line with the rest of the framework to support spaces there I think.

@RicardoRamirezR

Copy link
Copy Markdown

I think it looks nicer with spaces

@carestad

Copy link
Copy Markdown
Contributor

I am having some difficulties aliasing the primaryKey here. It does not seem to work with the closure.

Example:

MyModel::with(['relation'  => function($q) {
  $q->select('primaryId as id', 'foo as bar');
});

This will return null, while the following:

MyModel::with(['relation'  => function($q) {
  $q->select('primaryId', 'primaryId as id', 'foo as bar');
});

Will work, but will also return a primaryId key that I don't need.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants