dimanche 19 septembre 2021

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 :


    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
    

Aucun commentaire:

Enregistrer un commentaire