Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[12.x] Address Model@relationLoaded when relation is null #55531

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

Merged
merged 2 commits into from
Apr 24, 2025

Conversation

rodrigopedra
Copy link
Contributor

@rodrigopedra rodrigopedra commented Apr 24, 2025

This is a fix after PR #55519

As noted by @nicekiwi on this comment:

#55519 (comment)

When a relation returns null, for example $user->address, the $relatedModel array introduced on PR #55519, becomes [null].

That can lead to the following foreach to be executed, and fail as: Error: Call to a member function relationLoaded() on null

This PR:

  • Addresses this case by adding an array_filter() call to ensure no nulls are present
  • Adds another test case covering these changes

@decadence
Copy link
Contributor

decadence commented Apr 24, 2025

Just updated to 12.10.1 and got this error πŸ˜„ Thank you for quick fix.

@tabuna
Copy link
Contributor

tabuna commented Apr 24, 2025

Thank you! However, it seems that the issue still persists after this PR.

I have the following example to reproduce it:

$model = new class extends Model {};

$model->setRelations(['many' => ['one', 'two', 'three' => 84]]);

$result = $this->getAttribute('many.three');

// In version 12.10.1 (and with this PR), this throws:
// Error: Call to a member function relationLoaded() on null
// Previously, this would return null.

@taylorotwell taylorotwell merged commit f5fba9c into laravel:12.x Apr 24, 2025
58 checks passed
@taylorotwell
Copy link
Member

Can we just revert everything related to this?

@rodrigopedra
Copy link
Contributor Author

Can we just revert everything related to this?

I guess so. Mind both my PRs were trying to fix the original one.

Do you want me to send a reversal PR?

@nicekiwi
Copy link

I would like to keep the feature if it can work without issue, it would be useful.

@rodrigopedra
Copy link
Contributor Author

rodrigopedra commented Apr 25, 2025

@nicekiwi it was already reverted in 12.0.2

I agree it is useful, but it had too many oversights on edge-cases. (Granted that it was very hard for the original author to think about them all in advance.)

You can try sending a PR that addresses these edge cases. You can either build up from the reverted code and consider the bug reports, or start from scratch.

An alternative is creating a package that provides an override for that method. Even as a trait, as it would be added by a subclass from the model, it wouldn't conflict with the HasRelationships trait.

@AndrewMast
Copy link
Contributor

@nicekiwi It was reverted in this commit: dc5b445

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.

6 participants