Description
- Laravel-mongodb Version: 8
- PHP Version: 7.4
- Database Driver & Version:
Description:
Full example in this SO question: https://stackoverflow.com/questions/69260901/jensseger-belongstomany-returns-nothing-when-selecting-fields
Created a BelongsToMany relationship, tried to filter related manytomany with $readingList = ReadingList::where('user_id', $user->id)->with("stories:title")->get();, and it launches "Invalid argument supplied for foreach()", debugged,and it seems that in BelongsToMany.php, at line 248 in function buildDictionary() it tries to access 'reading_list_ids' pivot array, but it doesn't exist so launches a null, causing the error. If i just remove ":title" from the line, it works perfectly, and $result contains {"_id":"6148dc2a23ef6d0e6838h123","title":"Lorem","blurb":"Ipsum","completed":"1"} at that point, so it doesn't seems just a matter of not having the field. Noy sure if this is a bug, or if there is a way to make this work somewhat.
Steps to reproduce
- Create BelongsToMany relationship.
- Get the relation with a with("model:field").
- Get the error.
Expected behaviour
Should return the data normally, with the related model having only the requested fields.
Actual behaviour
It launches Invalid argument supplied for foreach() error