Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

171
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda