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

Exception thrown when calling whenPivotLoadedAs when preventAccessingMissingAttributes is active #44663

Closed
gerardnll opened this issue Oct 19, 2022 · 5 comments

Comments

@gerardnll
Copy link
Contributor

gerardnll commented Oct 19, 2022

  • Laravel Version: 9.36.1
  • PHP Version: 8.1.6
  • Database Driver & Version: not applicable

Description:

Having Model::preventAccessingMissingAttributes(true); and trying to make use of a JsonResource whenPivotLoadedAs() method results in a Illuminate\Database\Eloquent\MissingAttributeException. Is this an expected behaviour? Shouldn't the whenPivotLoadedAs method check for the relation existence by calling relationLoaded instead of trying to access the relationship directly?

I could do a PR with that change.

@keyur555
Copy link

I think you are right. My tests are failing because this too :)

@dpash
Copy link

dpash commented Oct 25, 2022

I'm also seeing issues. I think part of the problem is that Laravel loads foo_id as pivot_foo_id and gets confused because it's not foo_id.

@Victor-emil
Copy link
Contributor

I'm having the exact same issue in a JSON resource where I am using whenPivotLoaded to decide whether or not to include pivot fields.
Screenshot 2022-10-25 at 13 45 46
Screenshot 2022-10-25 at 13 46 45

@gdebrauwer
Copy link
Contributor

This has been fixed in v9.38.0, see #44792

@driesvints
Copy link
Member

Thanks all.

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

6 participants