title: How to Fetch data from multiple tables using relationships in laravel on certain conditions?
body: <p>I am fetching data from multiple tables by joining them and the relationship is one to many and one to one
  Everything works fine as I am using joins with raw method (directly writing queries) but I want to use Laravel sophistication which means I am looking to use with() has() or whereHas() to get same results.</p>
  <pre><code>                                          My Query that works fine in controller
  
      
  
  $licenses = DB::table('licenses')
              -&gt;join('users as u', 'u.id', '=', 'licenses.user_id')
              -&gt;join('users as sp', 'sp.id', '=', 'licenses.sales_person_id')
              -&gt;join('license_types', 'license_types.id', '=', 'licenses.license_type_id')
              -&gt;select('licenses.*', 'license_types.*', 'u.first_name', 'u.last_name', 'u.email', 'sp.first_name as fname', 'sp.last_name as lname')
              -&gt;where('u.first_name', 'LIKE', '%' . $query . '%')
              -&gt;orWhere('u.last_name', 'LIKE', '%' . $query . '%')
              -&gt;orWhere('u.email', 'LIKE', '%' . $query . '%')
              -&gt;orWhere('sp.first_name', 'LIKE', '%' . $query . '%')
              -&gt;orWhere('sp.last_name', 'LIKE', '%' . $query . '%')
              -&gt;get();
  
  
                                          My License Model
  
  public function user()  
  {
      return $this-&gt;hasOne('App\User','id','user_id');
  }
  public function license_type()
  {   
      return $this-&gt;hasOne('App\LicenseType','id','license_type_id');
  }
  
  
  
                  The Response I get when the above mentioned query is executed
   #items: array:1 [
  0 =&gt; {#444
    +&quot;id&quot;: 1
    +&quot;user_id&quot;: 2
    +&quot;sales_person_id&quot;: 3
    +&quot;license_type_id&quot;: 1
    +&quot;license&quot;: &quot;CCVTGS7S0R4M8P7R7S3R&quot;
    +&quot;license_duration&quot;: 23
    +&quot;license_expiry&quot;: null
    +&quot;allowed_test&quot;: &quot;fef&quot;
    +&quot;no_of_devices_allowed&quot;: 1
    +&quot;is_deleted&quot;: 0
    +&quot;trial_activated_at&quot;: &quot;2021-03-10 10:18:04&quot;
    +&quot;license_activated_at&quot;: null
    +&quot;user_device_unique_id&quot;: &quot;a9dc00sssd6daf79&quot;
    +&quot;is_active&quot;: 1
    +&quot;created_at&quot;: null
    +&quot;updated_at&quot;: null
    +&quot;title&quot;: &quot;monthly&quot;
    +&quot;price&quot;: &quot;232&quot;
    +&quot;type&quot;: 2
    +&quot;first_name&quot;: &quot;Mohammad&quot;
    +&quot;last_name&quot;: &quot;Fahad&quot;
    +&quot;email&quot;: &quot;apexlegendsisracistt@gmail.com&quot;
    +&quot;fname&quot;: &quot;Mohammad&quot;
    +&quot;lname&quot;: &quot;Fahad&quot;
  }
  </code></pre>
  <p>]</p>
  <p>I want to get the same results with the Laravel eloquent but when I use with I dont know how to specify where conditions and to get same output.</p>