Skip to content
Advertisement

LARAVEL: I am only getting one item from my table when there are multiple items. How would I get all of them and output them?

I am using a session to create a basic cart. In the session there are multiple IDs ranging from 1-3.

When I output the data using foreach it only outputs the first item from the table. I would like all the related items to appear. How would I go about doing that?

Here is my index function from my controller:

JavaScript

This is my output on the cart page:

JavaScript

And for testing purposes this is my dump and die of the $cartData:

JavaScript

And finally this out the actual output:

JavaScript

Advertisement

Answer

You have two issues:

  1. You’re passing in a string of IDs instead of an array
  2. where only looks for a single value.

So the query is actually becoming where id = '1,1,3', which may only match the id of 1. To fix it, pass in the actual array, and use whereIn:

JavaScript

This changes the query to where id IN (1,1,3).

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement