Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
How can I used "if" condition in "whereIn" in laravel

Below is my query which one i used and working fine for me.

    if($t_requested['category_id'])
    {
        $t_query_condition['p.category_id'] = $t_requested['category_id'];
    }
    if($t_requested['brand_id'])
    {
        $t_query_condition['p.brand_id'] = $t_requested['brand_id'];
    }

    $browseData = DB::table('products as p')
        ->leftjoin('categories as cp', 'p.category_id', '=', 'cp.id')
        ->leftjoin('brands as bp', 'p.brand_id', '=', 'bp.id')
        ->select('p.id as product_id','p.name as product_name','cp.title as category_name','bp.name as brand_name','p.product_weight',
               'p.short_description','p.product_price','p.special_price','p.stock_quantity')
        ->orderBy('p.created_by', "desc")
        ->offset(0)
        ->limit(10)
        ->where($t_query_condition)
        ->get();

but Now i have getting multiple id in "category_id" and "brand_id", i want to used whereIn but it used with of condition. if i get category_id or brand_id null then it's skip.

thanks in advance.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Try this query hope you will get result

        $browseData = DB::table('products as p')
       ->select('p.id as product_id','p.name as product_name','cp.title as category_name','bp.name as brand_name','p.product_weight',
               'p.short_description','p.product_price','p.special_price','p.stock_quantity')

        ->leftjoin('categories as cp', 'p.category_id', '=', 'cp.id')
        ->leftjoin('brands as bp', 'p.brand_id', '=', 'bp.id')
        ->orderBy('p.created_by', "desc");
       if($t_requested['category_id'])
         {
          $browseData->whereIn('p.category_id',$t_requested['category_id']);
         }
      if($t_requested['brand_id'])
         {
         $browseData->whereIn('p.brand_id',$t_requested['brand_id']);
         }

      $result=browseData->offset(0)->limit(10)->get();
over 4 years ago · Santiago Trujillo Report

0

I think it is very simple, you have to pass array of values instead of one value

I have tried to write options for achieve this, you have to follow which is suitable for you

if($t_requested['category_id'])
{
   $t_query_condition['p.category_id'] = $t_requested['category_id']; // it should be array OR

   $t_query_condition['p.category_id'] = explode(",",$t_requested['category_id']); // if you get value comma saperated

$t_query_condition['p.category_id'] = explode(",",$t_requested['category_id']); // if you get value comma saperated

$t_query_condition['p.category_id'] = [$t_requested['category_id']]; // if you want to convert one value into array
        }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!