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

637
Visualizações
laravel how to make a multiple filter using query in datatables using switch

i have 5 filter , kampus,fakulti,program ,semester and status so each filter is always been filter with status it means the status filter will be the selection of switch case and the other filter will be condition in the switch case example :

$student = Student::select("*");

    if($student){
      $today = date("Y-m-d");
      switch($request->stat){
        case 'Y':
          
          if($request->kampus != "-" && $request->fakulti != "-" && $request->program != "-" && $request->part != "-" ){
            $rs = $student->where('kodkampus',$request->kampus)
            ->where('kodfakulti',$request->fakulti)
            ->where('kodprogram',$request->program)
            ->where('part',$request->semester)
            ->where('vaccine_date', '<=', $today)
            ->where('vaccine2_date', '<=', $today);

          } else if ($request->kampus != "-" && $request->fakulti != "-"  && $request->program != "-" ){
            
            $rs = $student
            ->where('kodkampus',$request->kampus)
            ->where('kodprogram',$request->program)
            ->where('kodfakulti',$request->fakulti)

            ->where('vaccine_date', '<=', $today)
            ->where('vaccine2_date', '<=', $today);

          } else if ($request->kampus != "-" && $request->fakulti != "-"){
            $rs = $student->where('kodkampus',$request->kampus)
            ->where('kodfakulti',$request->fakulti)
            ->where('vaccine_date', '<=', $today)
            ->where('vaccine2_date', '<=', $today);
            
          } else if ($request->kampus != "-"){
            $rs = $student->where('kodkampus',$request->kampus)
            ->where('vaccine_date', '<=', $today)
            ->where('vaccine2_date', '<=', $today);
          }

          
          $rs = $rs->get();
          break;
        case 'D1':
          $rs = Student::get();
           break;
      }

      return datatables()
      ->of($rs)->make(true);

    }else{
      abort(404,'no record');
    }

so i need that the filter can filter individually without need to fulfill all the condition, for example if i want to filter only fakulti, i can without choosing kampus and program and semester. im seriusly need help and i need it to be individual filter but in the same query

please2 help me im stuck about 3 days, and i'm a new to laravel, using eloquent and datatable

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You dont have to do it with if else

It's a query builder, you can stack the wheres on the $student query builder

if(!$student){
    return abort(404,'no record');
}
$today = date("Y-m-d");
if ($request->stat == 'Y'){
  if($request->part != "-" ){
    $student->where('part',$request->semester)
  }
  if ($request->program != "-" ){
    $student->where('kodprogram',$request->program)
  }
  if ($request->fakulti != "-"){
    $student->where('kodfakulti',$request->fakulti);
  }
  if ($request->kampus != "-"){
    $student->where('kodkampus',$request->kampus)
    ->where('vaccine_date', '<=', $today)
    ->where('vaccine2_date', '<=', $today);
  }

  $rs = $student->get();
} else {
  $rs = Student::get();
}

return datatables()
->of($rs)->make(true);

Changed the switch for another if else because you dont want to end up with undefined variable $rs if $request->stat is neather Y or D1

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