title: Filter a one to many relation record result
body: <p>I need to filter the results given from a one to many relation directly from the model object, but I cant find the right way.
  Here, the relations:</p>
  
  <p>I have a User model that can subscript to many companies (company model), and a company can have many users subscripted to it, so, is a many to many relation.</p>
  
  <p>In every company, this user has a personal info, so, every user, could have many profiles (profile model) one for each company that is subscripted to.
  So It is a one to many relation.</p>
  
  <p>Imagine I want to retrieve the company that a user is currently viewing directly from the model, this, Im achieving by filtering the many to many relation:</p>
  
  <pre><code>class User extends Authenticatable
  {
  ///
   public function obtainLoggedOnCompany()
   {
     return $this-&gt;belongsToMany('app\Company', 'user_company', 'id', 'company_id')-&gt;withPivot('selected')-&gt;wherePivot('selected', 1)  
   }
  </code></pre>
  
  <p>Then, if I want to return the selected company in a blade view I just call:</p>
  
  <p>Auth::user()->obtainLoggedOnCompany->first();</p>
  
  <p>All thanks to the withPivot and wherePivot clause.</p>
  
  <p>Different is the case when I want to retrieve the registered profile of a currently selected company, I tried:</p>
  
  <pre><code>public function obtainSelectedProfile()
  {
  $SelectedCompany= $this-&gt;obtainLoggedOnCompany-&gt;first();
  
  return $this-&gt;hasMany('app\Profile','user_id')-&gt;where('company_id', '=', $SelectedCompany-&gt;company_id);
  }
  </code></pre>
  
  <p>But it throws a Trying to get property of non object Exception.
  Is there another way to filter one to many relations directly in the model relation function?</p>
  
  <p>Iam using Laravel 5.2</p>