Querying collections in Laravel
<p>Have I got it correctly that when I query a Laravel collection, it doesn't query the database but executes the query on what was already fetched?</p>

<p>For example, I have a relation that returns a collection:</p>

<pre><code>public function permissions()
{
    return $this-&gt;belongsToMany(Permission::class, RolePermission::getModelTable(), 'role_id', 'permission_id');
}
</code></pre>

<p>Does the following code query the database or it works with the collection using php facilities?</p>

<pre><code>$role-&gt;permissions-&gt;where('code','global.test')-&gt;count()
</code></pre>

<p>And, as far as I understand, if I query the relationship then the database will be queried instead of working with the results that were already fetched:</p>

<pre><code>$role-&gt;permissions()-&gt;where('code','global.test')-&gt;count()
</code></pre>

<p>So basically, <strong>$role->permissions</strong> - working with the fetched results "offline", but <strong>$role->permissions()</strong> - querying the database</p>

<p>What way is generally more efficient and when?</p>