skip to Main Content

enter image description here
I send an array with ajax included category_id as shown in image. And catch these categories in controller to execute product row equal with category_id field in products table. In this case, i cannot loop the array as category_id sending from ajax for where condition in controller. Pls guide me…….

public function productCategoryList(Request $request)
{
    logger($request->status);
    $data = Products::where('category_id', $request->status)->get();
    return $data;
}

2

Answers


  1. You could use whereIn() on your query builder:

    public function productCategoryList(Request $request)
    {
        logger($request->status);
        $data = Products::whereIn('category_id', $request->status)->get();
        return $data;
    }
    

    That would allow you to query a list of elements, instead of a single element.

    The documentation for how to use whereIn() can be found at https://laravel.com/docs/10.x/queries#additional-where-clauses

    Login or Signup to reply.
  2. You can use whereIn() that allows you to filter query results based on a given array of values.

    $data = Products::whereIn('category_id', $request->status)->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search