Skip to content

[5.6] Remove null relationships in loadMorph before grouped by class name - #24322

Merged
taylorotwell merged 2 commits into
laravel:5.6from
beamjet:loadMorph
May 25, 2018
Merged

[5.6] Remove null relationships in loadMorph before grouped by class name#24322
taylorotwell merged 2 commits into
laravel:5.6from
beamjet:loadMorph

Conversation

@beamjet

@beamjet beamjet commented May 25, 2018

Copy link
Copy Markdown
Contributor

Relationship model may be null when using with() to eager load them, which will cause error in loadMorph() because of the usage of get_class().
So, remove null relationships before group them by class name seems reasonable.

@browner12

Copy link
Copy Markdown
Contributor

could you please provide a more thorough description and use case of what this fixes?

also, probably needs to go to 5.7

@tillkruss tillkruss left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use filter() without a callback.

@beamjet

beamjet commented May 25, 2018

Copy link
Copy Markdown
Contributor Author

@tillkruss Thanks for your review. Use filter() without a callback is just much more concise.

@beamjet

beamjet commented May 25, 2018

Copy link
Copy Markdown
Contributor Author

@browner12 Here is a example:

$notifications = Auth::user()->notifications()
            ->with('subject')
            ->get()
            ->loadMorph('subject', [
                EventInvite::class => 'event',
                GroupPhotoUpload::class => 'group',
                GroupPost::class => 'group',
                PostLike::class => 'post',
            ]);

In this case, subject relationship of notifications are eager-loaded. But some notifications may have no subject relationship and subject model will be null in this case, which will cause error in loadMorph because it passes null to get_class().

@taylorotwell
taylorotwell merged commit 3976a43 into laravel:5.6 May 25, 2018
@GrahamCampbell GrahamCampbell changed the title Remove null relationships in loadMorph before grouped by class name. [5.6] Remove null relationships in loadMorph before grouped by class name May 26, 2018
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.

4 participants