Search code examples
laravellaravel-5laravel-5.3

Advanced Filters in laravel


In my case I'm working on search training institutes based on institute name,location and course

I have 3 tables

institutes Table

id   |   institute_name       |  phone_number
----------------------------------------------
 1   |     vepsun             |   85214542462
----------------------------------------------
 2   |     infocampus         |   52466475544

Locations table

id   | institute_id(fk) |    location_name        
------------------------------------------------
 1   |  1               |      Banglore
------------------------------------------------
 2   |  1               |       delhi

courses table

 id   | institute_id(fk) |    course_name        
------------------------------------------------
 1   |  1               |      php
------------------------------------------------
 2   |  1               |      delhi

I have created relations between 3 tables tables

Institute Model :

 public function locations()
 {
     return $this->hasMany(Location::class);
 }
  public function courses()
 {
     return $this->hasMany(Course::class);
 }

Course Model:

 public function institute()
 {
     return $this->belongsTo(Institute::class);
 }

Location model:

  public function institute()
  {
     return $this->belongsTo(Institute::class);
  }

So I have tried below code

  public function filter(Request $request)
  {
       $institute = (new Institute)->newQuery();

       // Search for a user based on their institute.

       if ($request->has('institute_name')) {
       $institute->where('institute_name', $request->input('institute_name'));
       }

      // Search for a user based on their course_name.

      if ($request->has('course_name')) {
      $institute->whereHas('courses', function ($query) use ($request) {
      $query->where('courses.course_name', $request->input('course_name'));
      });
      }

      // Search for a user based on their course_name.           

      if ($request->has('location_name')) {
      $institute->whereHas('locations', function ($query) use ($request) {
      $query->where('locations.location_name', $request->input('location_name'));
      });
      }
      return response()->json($institute->get());
  }

From above code i'm able to filter the data but it show only institution table data like below.

  [
      {
          "id": 2,
          "institute_name": "qspider",
          "institute_contact_number": "9903456789",
          "institute_email": "[email protected]",
          "status": "1",
      }
  ]

but what I need is when I do seach with course_name or instute_name I need to fetch data from institues table,courses and locations table. Can anyone help on this, please?


Solution

  • Eager load the relations in your if statements.

      // Search for a user based on their course_name.
      if ($request->has('course_name')) {
          $institute->whereHas('courses', function ($query) use ($request) {
              $query->where('courses.course_name', $request->input('course_name'));
          })
          ->with('courses); // <<<<<< add this line
      }
    
      // Search for a user based on their course_name.           
      if ($request->has('location_name')) {
          $institute->whereHas('locations', function ($query) use ($request) {
              $query->where('locations.location_name', $request->input('location_name'));
          })
          ->with('locations'); // <<<<<<< add this line
      }
    

    This will fetch related courses and locations as well.