Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

mkrahamath's avatar

Query is takes only first id

I wanted to show the products only from mentioned by using below query but which is showing only first one.

$productIds = "1,2,3,5,6,8";

$query = GP::table('gp_products')
                ->select('gp_products.*', 'gp_product_details.*', 'gp_product_status.*')
                ->join('gp_product_details', 'gp_product_details.product_id', '=', 'gp_products.product_id')
                ->leftJoin('gp_product_status', 'gp_product_status.product_id', '=', 'gp_products.product_id')
                ->whereIn('gp_products.product_id', [$productIds])
                ->where([
                    ['gp_product_details.locale', '=', 'en_US'],
                    ['gp_products.type', '=', 'dynamic'],
                    ['gp_products.status', '=', 1]
                ])
                ->orderByRaw('gp_products.position - gp_products.updated_at DESC')
                ->get();

    return $query;
0 likes
12 replies
NOMGUY's avatar

Do it,

->orderByRaw('gp_products.position - gp_products.updated_at', 'DESC')
mkrahamath's avatar

Thanks but the problem is here, because i removed that line all products are showing.

->whereIn('gp_products.product_id', [$productIds])
automica's avatar

You’ve defined your product ids as a string before passing that string to your query and wrapping it in brackets. Just create the array of ids first, and pass that in.

1 like
NOMGUY's avatar
NOMGUY
Best Answer
Level 16

Then do it,

$productIds = [1,2,3,5,6,8];

After that,

->whereIn('gp_products.product_id', $productIds)
1 like
automica's avatar

If you are only getting one record when passing in the array, check you actually have products with those ids in your database.

Installing a package such as Tracy will give you access to queries you are running in your UI which will allow scrutinise what’s running at db layer https://packagist.org/packages/recca0120/laravel-tracy

NOMGUY's avatar

instead of

return $query;

try

echo "<pre>";
print_r($query);
die;

for getting the products. and tell what are you getting.

mkrahamath's avatar

I did following like said by @brightstormhq and it worked.

$productIds = "1,2,3,5,6,8";
$ids = explode(',', $productIds);

And

->whereIn('gp_products.product_id', $ids)
automica's avatar

@mkrahamath you can simplify the above by just setting your

$productIds = [1,2,3,5,6,8];

You are initially declaring a string of numbers and then turning that into an array when you could easily already define the array in the first place

Can you mark NOMGUYs answer as best answer so he picks up the credit for it?

Please or to participate in this conversation.