title: advanced filters in laravel
body: <p>In my case, I have to filter the data by city or institute</p>
  
  <p>I have two tables like below </p>
  
  <pre><code> Institutes table
  
   id  |  institute_name |  phoneNumber
   -------------------------------------
    1      infocampus         9999999999
   -------------------------------------
    2      jspider            2348234982
  
   courses table
  
   id  |   institute_id    |     course_name
   ------------------------------------------
   1             1                 java
   2             1                 php
  </code></pre>
  
  <p>Relations that I have created</p>
  
  <pre><code>Institue model
  
  public function courses()
  {
      return $this-&gt;hasMany(Course::class);
  }
  
  Course model
  
  public function institute()
  {
       return $this-&gt;belongsTo(Institute::class);
  }
  </code></pre>
  
  <p>I have tried with below code </p>
  
  <pre><code>public function filter(Request $request)
  {
      $institute = (new Institute)-&gt;newQuery();
  
      // Search for a user based on their institute.
  
      if ($request-&gt;has('institute_name')) {
      $institute-&gt;where('institute_name', $request-&gt;input('institute_name'));
      }
  
      // Search for a user based on their course_name.
  
      if ($request-&gt;has('course_name')) {
      $institute-&gt;whereHas('courses', function ($query) use ($request) {
          $query-&gt;where('courses.course_name', $request-&gt;input('course_name'));
      });
      }
  
      return response()-&gt;json($institute-&gt;get());
  }
  </code></pre>
  
  <p>From above i able to filter the data but it show only institution table data like below</p>
  
  <pre><code>[
  {
      "id": 2,
      "institute_name": "qspider",
      "institute_contact_number": "9903456789",
      "institute_email": "qspider@gmail.com",
      "status": "1",
      "created_at": null,
      "updated_at": null
  }
  ]
  </code></pre>
  
  <p>but what I need is when I do seach with course_name or instute_name I need to fetch data from institue table as well as courses table data.
  Can anyone help on this, please?</p>