Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

170
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda