Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.3] Allow loading specific columns while eagerloading #16327

Merged
merged 3 commits into from
Nov 11, 2016
Merged

[5.3] Allow loading specific columns while eagerloading #16327

merged 3 commits into from
Nov 11, 2016

Conversation

themsaid
Copy link
Member

@themsaid themsaid commented Nov 8, 2016

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
Contributor

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

@themsaid
Copy link
Member Author

themsaid commented Nov 8, 2016

@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
Copy link

kJamesy commented Nov 16, 2016

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
Copy link

RicardoRamirezR commented Nov 22, 2016

@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
Member

I think it looks nicer without spaces tbh.

@GrahamCampbell
Copy link
Member

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

@RicardoRamirezR
Copy link

I think it looks nicer with spaces

@carestad
Copy link
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.

None yet

7 participants