Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
peymanaslani70 wants to merge 4 commits into mongodb:5.x
base: 5.x
Choose a base branch
Loading
from peymanaslani70:fix-belongs-to-many-relation-with-object-id

Conversation

Copy link

@peymanaslani70 peymanaslani70 commented Mar 6, 2025
edited
Loading

🚀 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

  • Previously, BelongsToMany expected foreign keys to be a single value, causing issues when an array was used.
  • This resulted in errors like Illegal offset type when working with MongoDB.

🔍 Issue Reference
Fixes [#3015]

🔧 Changes Made

✅ Updated attach() in BelongsToMany.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 (in tests/RelationsTest.php).

  • Ensures that array-based foreign keys work as expected.
  • Confirms that relationships retrieve the correct data.

🧪 How to Test It?

  1. Run the tests with:
    vendor/bin/phpunit tests/RelationsTest.php
  2. All tests should pass ✅

📜 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

  • Add tests and ensure they pass
  • Updated Many-to-Many Relationship section in the docs.

alirezaudev reacted with thumbs up emoji
@jmikola jmikola requested review from GromNaN and removed request for jmikola and norareidy March 6, 2025 16:12
Copy link
Member

@GromNaN GromNaN left a comment
edited
Loading

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

Copy link
Author

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

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@GromNaN GromNaN GromNaN requested changes

Requested changes must be addressed to merge this pull request.

Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /