title: In Laravel how to pass parameters to a BelongsToMany
body: <p>I have this relation in my User model:</p>
  
  <pre><code>public function groups() {
    return $this-&gt;belongsToMany('\App\Group', 'role_in_group')
                    -&gt;orderBy('role_id', 'desc');
  }
  </code></pre>
  
  <p>Obviously it returns the groups a user belongs to. So far, so good. But I have this role_in_group pivot table, I want to get the groups a user belongs to, but with an specific role, so I wrote this other function:</p>
  
  <pre><code>public function groupsAsAdmin() {
    return $this-&gt;belongsToMany('\App\Group', 'role_in_group')
                    -&gt;whereIn('role_id', [15, 18, 19])
                    -&gt;orderBy('role_id', 'desc');
  }
  </code></pre>
  
  <p>And it works, but I think its a silly solution, I want to do something like:</p>
  
  <pre><code>public function groups(array $roles_id = NULL) {
  
    if (empty($roles_id)) {
      $id = [10, 15, 18, 19];
    }
  
    return $this-&gt;belongsToMany('\App\Group', 'role_in_group')
                    -&gt;whereIn('role_id', $id)
                    -&gt;orderBy('role_id', 'desc');
  }
  </code></pre>
  
  <p>I <strong>know</strong> this must work. My problem is in the template.</p>
  
  <p>If I ask for <code>$user-&gt;groups</code> it will return a collection, which is what I want, but if I ask for <code>$user-&gt;groups([19])</code> it will return a <code>belongsToMany</code> object, so it wont work inside <code>@foreach</code></p>
  
  <p>This will fail:</p>
  
  <pre><code>@foreach($user-&gt;groups([10, 15, 19]) as $group)
     {{ $group-&gt;id }}&lt;br&gt;
  @endforeach
  </code></pre>
  
  <p>So, how can I call the <code>groups()</code> function in the model, and make it return a collection, as it would if I call it without the parenthesis? Is there a better way other than having two functions as I have it now?</p>