Laravel: Where selection for Eloquent Eager Loading relationship : <p>I got two DB tables:</p>

<p><strong>Posts</strong></p>

<pre><code>$table-&gt;increments('id');
$table-&gt;integer('country_id')-&gt;unsigned();
$table-&gt;foreign('country_id')-&gt;references('id')-&gt;on('countries');
</code></pre>

<p><strong>Countries</strong> </p>

<pre><code>$table-&gt;increments('id');
$table-&gt;string('name', 70);
</code></pre>

<p>I use laravel as back-end. Now I want to implement filtering data for my front-end. So the user can select a country name and laravel should answer the request only with posts that have a country with the specified name.</p>

<p>How could I add this condition to my existing pagination query? I tried this:</p>

<pre><code>$query = app(Post::class)-&gt;with('country')-&gt;newQuery(); 
// ...
if ($request-&gt;exists('country')) {
        $query-&gt;where('country.name', $request-&gt;country);
}
// ...
</code></pre>

<p>... resulting in the following error:</p>

<pre><code>Column not found: 1054 Unknown column 'country.name' in 'where clause' (SQL: select count(*) as aggregate from `posts` where `country`.`name` = Albania)
</code></pre>
