-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix BelongsToMany Relation to Support Array Foreign Keys in Laravel MongoDB #3310
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
Fix BelongsToMany Relation to Support Array Foreign Keys in Laravel MongoDB #3310
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay in the review, it's because I don't understand the feature implemented.
The new test passes, even without the BelongsToMany
modification. And the data stored in the database is identical with or without your modification. Which makes me think that the test doesn't cover the intended functionality.
Tested with Laravel 12.11.0, the user document contains an array of ids represented in with string
type.
image
Jira tracking: PHPORM-116
Sorry for the delay in the review, it's because I don't understand the feature implemented. The new test passes, even without the
BelongsToMany
modification. And the data stored in the database is identical with or without your modification. Which makes me think that the test doesn't cover the intended functionality.Tested with Laravel 12.11.0, the user document contains an array of ids represented in with
string
type. imageJira tracking: PHPORM-116
Thanks a lot for the detailed feedback! 🙏
I understand the points you've mentioned and will work on aligning the implementation with the package's structure. I'll also add a dedicated test case as suggested.
Will update the PR soon.
Uh oh!
There was an error while loading. Please reload this page.
🚀 What’s in This PR?
This PR fixes the
BelongsToMany
relationship to support array-based foreign keys, allowing multiple related ObjectIds to be stored in a single field.🐞 The Issue
BelongsToMany
expected foreign keys to be a single value, causing issues when an array was used.Illegal offset type
when working with MongoDB.🔍 Issue Reference
Fixes [#3015]
🔧 Changes Made
✅ Updated
attach()
inBelongsToMany.php
to store ObjectIds as an array instead of a single value.✅ Modified
buildDictionary()
to correctly handle arrays when resolving relationships.✅ Added a new unit test:
testBelongsToManyRelationSupportsArrayForeignKeys
(intests/RelationsTest.php
).🧪 How to Test It?
📜 Documentation Update Checklist
✔️ Updated Many-to-Many Relationship section in the docs.
✔️ Explained how to use array-based foreign keys with
BelongsToMany()
.✔️ Added an example showing how to define relationships with an array field.
Checklist