Skip to content

Commit 0518aaa

Browse files
author
Peyman Aslani
committed
Updated documentation for array support in BelongsToMany relationships
1 parent 176d854 commit 0518aaa

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

docs/eloquent-models/relationships.txt

+20
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,26 @@ in the Laravel documentation.
255255
The following section shows an example of how to create a many to many
256256
relationship between model classes.
257257

258+
.. note::
259+
260+
In previous versions, the `foreignPivotKey` in a `BelongsToMany` relationship
261+
was expected to be a single `ObjectId` or `string`. However, in some MongoDB
262+
schemas, it is common to store multiple related IDs as an array. As of this update,
263+
`BelongsToMany` now supports array values for the `foreignPivotKey`.
264+
265+
Example:
266+
267+
.. code-block:: php
268+
269+
class User extends Model {
270+
public function roles() {
271+
return $this->belongsToMany(Role::class, null, 'role_ids', '_id');
272+
}
273+
}
274+
275+
Here, `role_ids` can be an array of `ObjectId`s, and the relationship will work correctly.
276+
277+
258278
Many to Many Example
259279
~~~~~~~~~~~~~~~~~~~~
260280

0 commit comments

Comments
 (0)