title: where clause inside a relationships - laravel
body: <p>I have 3 models like this:</p>
  
  <p>WarehousePivotCategory:</p>
  
  <pre><code> id - warehouse_id - warehouse_category_id
  </code></pre>
  
  <p>Warehouse:</p>
  
  <pre><code>title
  </code></pre>
  
  <p>WarehouseCategory:</p>
  
  <pre><code> title_en
  </code></pre>
  
  <p>I've created 2 <code>hasOne</code> relationships inside <code>WarehousePivotCategory</code> and they work fine:</p>
  
  <pre><code>  public function Warehouse()
    {
    return $this-&gt;hasOne('App\Models\Warehouse','id','warehouse_id');
    }
  
    public function WarehouseCategory()
    {
    return $this-&gt;hasOne('App\Models\WarehouseCategory','id','warehouse_category_id');
    }
  </code></pre>
  
  <p>in the database I have two records in <code>warehouses</code> table :</p>
  
  <pre><code>  id title
    1 AA
    2 BB
  </code></pre>
  
  <p>I want to search title in <code>warehouses</code> :</p>
  
  <pre><code>$title = 'AA';
  
  
  
  $warehouses = WarehousePivotCategory::with(['warehouse' =&gt; function($q) use ($title) {
      $q-&gt;where('title', 'like', '%' . $title . '%');
  },'WarehouseCategory'])-&gt;get();
  
  
      foreach ($warehouses as $w)
      {
          echo $w-&gt;warehouse-&gt;title; // no thing
  
      }
  </code></pre>
  
  <p>but it doesn't return any of <code>title</code> of warehouses.</p>
  
  <p>my relationships is correct because below code works fine :</p>
  
  <pre><code>WarehousePivotCategory::with('warehouse','WarehouseCategory')-&gt;paginate(10);
  </code></pre>