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.2] Correctly load nested relationships for polymorphic relations #13737

Merged

Conversation

phroggyy
Copy link
Contributor

Firstly, this fixes a bug that was created by #13724 whereby eager loads through protected $with would be ignored for polymorphic relations.

When writing tests for the fix, it was discovered that nested relationships are in fact never loaded for polymorphic relations. So that works too now.

This means you can now have something like

class Like extends Model
{
    protected $with = [‘likeable.owner’];

    public function likeable()
    {
        return $this->morphTo();
    }
}

class Post extends Model
{
    public function owner()
    {
        return $this->belongsTo(User::class);
    }
}

@markvaneijk
Copy link
Contributor

Great work, thanks!

@joshbodine21
Copy link

Was this not merged into 5.3?

@GrahamCampbell
Copy link
Member

It was?

@joshbodine21
Copy link

I will need to dig deeper then. It was working great in 5.2.34 and I just upgrade to 5.3 and it no longer seems to work.

@joshbodine21
Copy link

I still can't get this to work after upgrading to 5.3. After downgrading, the same code works fine again. Looks like 5.3 still has an issue.

@almas1992
Copy link

How can I use whereHas() method ?
like this.....

Like::whereHas('likeable', function ($query) {
			$query->where('id', 0);
		})->get();

@sebastiaanluca
Copy link
Contributor

sebastiaanluca commented May 12, 2017

If the likeable morph model could consist of a post, comment, and a third user model, and only post has the owner relation, what should be the result when trying to load owner on all likeables?

Currently this would throw an exception:

Call to undefined relationship [owner] on model (comment and user)

My goal would be to have something like this in the parent model:

 protected $with = [
    'likeable.owner',
    'likeable.other',
];

Where it would load the owner relation for the post and other for whichever likeable model implements it, ignoring relations that don't exist on morphed models.

@saqueib
Copy link

saqueib commented Oct 30, 2017

@joshbodine21 did you manage to get it working, I am using Laravel 5.4.36 and not able to load nested relation. I have tried

protected $with = [
    'subject.attachments'
];

and tried directly calling it from query builder

Activity::with('subject.attachments', 'causer')->latest()->get()

Here is an issue which I opened spatie/laravel-activitylog#281
@GrahamCampbell @freekmurze @phroggyy please help on this.

@skcin7
Copy link

skcin7 commented Mar 23, 2018

I'm currently facing the same issue that @saqueib is having. I'm on Laravel 5.5.26.

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.

9 participants