-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
BelongsToMany can't filter fields #2317
Comments
same is happening to me. any news? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
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
Logs:
local.ERROR: Invalid argument supplied for foreach() {"exception":"[object] (ErrorException(code: 0): Invalid argument supplied for foreach() at /var/www/vendor/jenssegers/mongodb/src/Relations/BelongsToMany.php:258) [stacktrace] #0 /var/www/vendor/jenssegers/mongodb/src/Relations/BelongsToMany.php(258): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(2, 'Invalid argumen...', '/var/www/vendor...', 258, Array) #1 /var/www/vendor/illuminate/database/Eloquent/Relations/BelongsToMany.php(276): Jenssegers\\Mongodb\\Relations\\BelongsToMany->buildDictionary(Object(Illuminate\\Database\\Eloquent\\Collection)) #2 /var/www/vendor/illuminate/database/Eloquent/Builder.php(649): Illuminate\\Database\\Eloquent\\Relations\\BelongsToMany->match(Array, Object(Illuminate\\Database\\Eloquent\\Collection), 'stories') #3 /var/www/vendor/illuminate/database/Eloquent/Builder.php(619): Illuminate\\Database\\Eloquent\\Builder->eagerLoadRelation(Array, 'stories', Object(Closure)) #4 /var/www/vendor/illuminate/database/Eloquent/Builder.php(587): Illuminate\\Database\\Eloquent\\Builder->eagerLoadRelations(Array) #5 /var/www/app/Http/Controllers/ReadingListController.php(138): Illuminate\\Database\\Eloquent\\Builder->get() #6 /var/www/vendor/illuminate/container/BoundMethod.php(36): App\\Http\\Controllers\\ReadingListController->listReadingLists() #7 /var/www/vendor/illuminate/container/Util.php(40): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}() #8 /var/www/vendor/illuminate/container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure(Object(Closure)) #9 /var/www/vendor/illuminate/container/BoundMethod.php(37): Illuminate\\Container\\BoundMethod::callBoundMethod(Object(Laravel\\Lumen\\Application), Array, Object(Closure)) #10 /var/www/vendor/illuminate/container/Container.php(651): Illuminate\\Container\\BoundMethod::call(Object(Laravel\\Lumen\\Application), Array, Array, NULL) #11 /var/www/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(386): Illuminate\\Container\\Container->call(Array, Array) #12 /var/www/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(352): Laravel\\Lumen\\Application->callControllerCallable(Array, Array) #13 /var/www/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(326): Laravel\\Lumen\\Application->callLumenController(Object(App\\Http\\Controllers\\ReadingListController), 'listReadingList...', Array) #14 /var/www/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(279): Laravel\\Lumen\\Application->callControllerAction(Array) #15 /var/www/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(259): Laravel\\Lumen\\Application->callActionOnArrayBasedRoute(Array) #16 /var/www/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(48): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #17 /var/www/app/Http/Middleware/Authenticate.php(71): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #18 /var/www/vendor/illuminate/pipeline/Pipeline.php(167): App\\Http\\Middleware\\Authenticate->handle(Object(Laravel\\Lumen\\Http\\Request), Object(Closure)) #19 /var/www/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(30): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #20 /var/www/vendor/illuminate/pipeline/Pipeline.php(103): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #21 /var/www/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(423): Illuminate\\Pipeline\\Pipeline->then(Object(Closure)) #22 /var/www/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(260): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure)) #23 /var/www/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(166): Laravel\\Lumen\\Application->handleFoundRoute(Array) #24 /var/www/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(48): Laravel\\Lumen\\Application->Laravel\\Lumen\\Concerns\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #25 /var/www/app/Http/Middleware/CorsMiddleware.php(29): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #26 /var/www/vendor/illuminate/pipeline/Pipeline.php(167): App\\Http\\Middleware\\CorsMiddleware->handle(Object(Laravel\\Lumen\\Http\\Request), Object(Closure)) #27 /var/www/vendor/laravel/lumen-framework/src/Routing/Pipeline.php(30): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #28 /var/www/vendor/illuminate/pipeline/Pipeline.php(103): Laravel\\Lumen\\Routing\\Pipeline->Laravel\\Lumen\\Routing\\{closure}(Object(Laravel\\Lumen\\Http\\Request)) #29 /var/www/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(423): Illuminate\\Pipeline\\Pipeline->then(Object(Closure)) #30 /var/www/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(172): Laravel\\Lumen\\Application->sendThroughPipeline(Array, Object(Closure)) #31 /var/www/vendor/laravel/lumen-framework/src/Concerns/RoutesRequests.php(109): Laravel\\Lumen\\Application->dispatch(NULL) #32 /var/www/public/index.php(28): Laravel\\Lumen\\Application->run() #33 {main} "}The text was updated successfully, but these errors were encountered: