Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vipin93's avatar
Level 13

How to filter data individually and combined?

I have filter function in my app and i want like if i submit the form with input "course" then it show result according to course_id = request->course, if he submit the form with year input then it show result according to "year_of_addmission = request->year", if he submit the form both input "year" and "course" then result show according to "course_id=request->course" and "year_of_addmission=request->year"

my controller

$search = $request->search;
        $course = $request->course;
        $year = $request->year;

       if($search)
       {
          
          $students = Student::search($search)->paginate(15);
          //$students = Student::with('city')->paginate(15);
       }


       elseif (($course) && ($year))
        {
         $students = Student::whereHas('course_id',$course)->orWhere('year_of_admission',$year)->paginate(15);
        }

and view

<form action="" method="get" class="form-inline">
             <div class="form-group">
              <select  id="course" class="form-control" name="course" >
                <option value="">---Select Course</option>
               @foreach($courses as $key=>$value)
                 
                <option value="{{ $key }}">{{ $value }}</option>
                @endforeach
              </select>  
              <select  id="year" name="year" class="form-control">
                <option value="">---Select year</option>
               @foreach($yearoptions as $yearoption)
                 
                <option value="{{ $yearoption->year_count }}">{{ $yearoption->year_count }}</option>
                @endforeach
              </select>
             </div>           
             <button type="submit">Submit</button>
            </form> 
0 likes
0 replies

Please or to participate in this conversation.