Search code examples
laravelif-statementeloquentswitch-statementlaravel-7

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


Solution

  • 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