title: Laravel - Add additional where constraints from Eloquent Model belongsToMany relationship
body: <p>I am new to Laravel (using v7) so I apologise if I am doing this incorrectly.
  I have a custom subscription setup for my users.</p>
  <p>I have my users table</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>id</th>
  <th>first_name</th>
  <th>last_name</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>John</td>
  <td>Doe</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Jane</td>
  <td>Doe</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>I have my subscriptions table</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>id</th>
  <th>name</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>Basic</td>
  </tr>
  <tr>
  <td>2</td>
  <td>Bronze</td>
  </tr>
  <tr>
  <td>3</td>
  <td>Silver</td>
  </tr>
  <tr>
  <td>4  ​</td>
  <td>Gold</td>
  </tr>
  <tr>
  <td>5  ​</td>
  <td>Platinum</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>And a subscription_user  pivot table</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>id</th>
  <th>user_id</th>
  <th>subscription_id</th>
  <th>created_at</th>
  <th>expired_at</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>1</td>
  <td>1</td>
  <td>1</td>
  <td>2021-02-01 23:22:12</td>
  <td>2021-03-21 08:22:12</td>
  </tr>
  <tr>
  <td>2</td>
  <td>1</td>
  <td>5</td>
  <td>2021-03-21 08:22:12</td>
  <td>2021-04-04 09:03:21</td>
  </tr>
  <tr>
  <td>2</td>
  <td>1</td>
  <td>3</td>
  <td>2021-04-04 09:03:21</td>
  <td></td>
  </tr>
  <tr>
  <td>2</td>
  <td>2</td>
  <td>1</td>
  <td>2021-01-01 01:00:00</td>
  <td>2021-01-05 05:30:00</td>
  </tr>
  <tr>
  <td>2</td>
  <td>2</td>
  <td>2</td>
  <td>2021-01-05 05:30:00</td>
  <td>2021-01-06 08:34:10</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>So as per the above entries, <code>John Doe</code> subscribed on the Basic subscription, then upgraded to the Platinum and then finally downgrading to the Silver. Which is his current subscription.</p>
  <p>I have the following methods (within User.php) for first getting a list of the users subscriptions</p>
  <pre><code>public function subscriptions()
  {
    return $this-&gt;belongsToMany('App\Subscription')
      -&gt;orderby('pivot_created_at', 'desc')
      -&gt;withPivot('created_at', 'expired_at')
      -&gt;withTimestamps();
  }
  </code></pre>
  <p>And then to get the current subscription</p>
  <pre><code>public function subscription()
  {
    return $this-&gt;subscriptions-&gt;first();
  }
  </code></pre>
  <p>However there could be instances where by a user has all subscriptions expired, therefore they have no current subscription in place. i.e in the above records <code>Jane Doe</code> who subscribed to 2 plans but both have expired.</p>
  <p>If i call <code>$user-&gt;subscription()</code> on Jane Doe it still returns the latest expired entry.
  I have tried added <code>where()</code> clauses into my <code>subscription</code> method but it has no effect:</p>
  <pre><code>public function subscription()
  {
    return $this-&gt;subscriptions-&gt;where('expired_at', NULL)-&gt;first();
  }
  </code></pre>
  <p>Is there a better way to do what I am trying to achieve? I need a method to fetch all the users subscriptions, whether they are active or expired. (which I have - is this the best way?)</p>
  <p>But then also fetch the current subscription which should always not have an <code>expired_at</code>.
  It seems I cannot add any further conditions within mt <code>subscription</code> method.</p>