title: Laravel relation search function
body: <p>I know where the following error is coming from but I do not know how to fix it.</p>
  
  <p>The error:</p>
  
  <blockquote>
    <p>SQLSTATE[42S22]: Column not found: 1054 Unknown column 'expenses.date' in 'where clause' (SQL: select count(*) as aggregate from <code>expenses_sections</code> where <code>expenses</code>.<code>date</code> LIKE %2015-12%)</p>
  </blockquote>
  
  <p>What is causing it (in my controller):</p>
  
  <pre><code>if($view === 'section') {
      $query = Section::query();
          $query-&gt;with(['expenses' =&gt; function($query) use ($search){
              $query-&gt;where('date', 'LIKE', '%2015-' . $search . '%')-&gt;get();
          }]);
  }
  </code></pre>
  
  <p>I have a select menu which I use to change the layout of the page. I have another select menu which I use to display results from a certain month only. The above error is what I am getting when I select a month, because the query is looking in the wrong table (because of the above code). The results are displayed using a relation between Expenses and Sections. What I basically want is to search for results in the <code>expenses</code> table, instead of the <code>expenses_sections</code> table, ofcourse without breaking the relation, because based on the relation, my layout is being displayed.</p>
  
  <p>Expense Model:</p>
  
  <pre><code>class Expense extends Model
  {
      public function section()
      {
          return $this-&gt;belongsTo('App\Http\Models\Expenses\Section');
      }
  }
  </code></pre>
  
  <p>Section Model:</p>
  
  <pre><code>class Section extends Model
  {
      public function expenses()
      {
          return $this-&gt;hasMany('App\Http\Models\Expenses\Expense');
      }
  }
  </code></pre>