title: Laravel - conditional filtering with relation table
body: <p>I have to conditionally filter results in the below query, based on <code>$search</code> is present or not.
  Below is my query,</p>
  <pre><code>    Sellers::with(
          'shop:id,width,height,business_id'
      )-&gt;with(
          'unit.location:id,name as l_name'
      )-&gt;select(
          'sellers.id', 'sellers.name' , 'address'
      )-&gt;when($search, function ($query, $search) {
           $query-&gt;where('l_name', 'like', '%' . $search . '%');
      })
      -&gt;findOrFail($request-&gt;seller_id);
  </code></pre>
  <p>The relations are,</p>
  <p>Sellers has many units.
  Units has one location.</p>
  <p>I need to filter the above result for location names based on the availability of search param.
  I have tried adding aliases, but they are throwing errors.</p>
  <p>How can this be attained?</p>