1

I am trying to fetch multiple rows of selected IDs from db. Here is my controller code

$products = new product;
$products = $products->select('products.*', 'categories.category_name')->join('categories', 'products.product_category', '=', 'categories.id');
$products = $products->whereIn('products.id', [$request->list]); //$request->list is post value (12,13)
$products = $products->get();

Here $request->list is post value which contains 12,13. Mentioned code works fine if I manually type IDs like this.

$products = $products->whereIn('products.id', [12,13]); 

But if I try to call same with variable or directly with request post then it is returning only one result.

$products = $products->whereIn('products.id', [$request->list]); 
//OR
$id = $request->list;
$products = $products->whereIn('products.id', $id); 

Why it is giving only one result when I use variable, any idea?

1 Answer 1

1

$request->list contains comma separated values, so convert it to an array. Change it as below:

$products = $products->whereIn('products.id', explode(',',$request->list)); 

Here is a full query:

$products = product::select('products.*', 'categories.category_name')
->join('categories', 'products.product_category', '=', 'categories.id')
->whereIn('products.id',explode(',',$request->list))
->get();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.