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

Polymorphic eager loading with select not working (correctly) #2966

Closed
rogierborst opened this issue Dec 16, 2013 · 8 comments
Closed

Polymorphic eager loading with select not working (correctly) #2966

rogierborst opened this issue Dec 16, 2013 · 8 comments

Comments

@rogierborst
Copy link

I have three tables, 'actors', 'series' and 'movies'. An actor can belong to many series and/or many movies, and both movies and series can have many actors. Good case for a polymorphic many-to-many relationship.

When I query all movies, I wish to eager load all actors, but only their first and last names. For this I do the following:

return Movie::with(array('actors' => function($query){
    $query->select('first_name', 'last_name');
}))->get();

This however still loads ALL columns for actors, since the resulting query is this one:

// showing only the relevant part of the query that was built
... select `first_name`, `last_name`, `actors`.* ... from `actors`

The issue here, is that the columns I requested (first_name and last_name) are indeed inserted into the query (first two fields), but then the actors.*` is automatically added, which obviously loads the rest of the columns as well.

I'm not smart enough to figure out how Laravel actually builds this query, so I'm not at all able to contribute with a pull request. Sorry.

@taylorotwell
Copy link
Member

We don't currently support limiting the selects on those relations because if it's not done properly it can break the query. You can remove the columns once the data has been retrieved if you like.

@fmmajd
Copy link

fmmajd commented Jan 17, 2018

is it still an issue? or is there support for such things now?

@websanova
Copy link
Contributor

Is there any way to set visible hidden properties on the relation directly then?

@staudenmeir
Copy link
Contributor

This has been fixed in Laravel 5.7.6 (#25662).

@lk77
Copy link

lk77 commented Oct 26, 2018

Hello,

#25662 introduce a bug,
we have thousand of queries using the with 'relation:field' syntax with polymorphic relations,
and it's not working anymore, the relation is now empty.
i don't know why, but it dont happen in 5.7.5, only until 5.7.6

work :

->with('morphTo');

don't work :

->with('morphTo:id,label');

if you have an idea, thanks.

@acacha
Copy link

acacha commented Dec 9, 2018

ping to @staudenmeir

@staudenmeir
Copy link
Contributor

@acacha Why?

@lk77
Copy link

lk77 commented Dec 11, 2018

@acacha if the ping is because of my post, it has been resolved.

the precise syntax we were using was :

->with('morphTo:label');

without the id (contrary to what i said earlier)

Since 5.7.6, the select clause is applied, it was not the case in 5.7.5.
It fails because of the missing id, and it breaks the app.
It has been reworked and solved for us.

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

No branches or pull requests

7 participants