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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x