How to add where condition to eloquent model on relation?
<p>I want to add where condition to my Model when with('category') is called. My relations are like this:


<pre><code>class Post extends Model
{
    public function category()
    {
        return $this-&gt;belongsTo(Category::class);
    }
}
</code></pre>

<p>Now I use this code to display post categories:</p>

<pre><code>Post::where('slug', $slug)-&gt;with('category')-&gt;get();
</code></pre>

<p>I want to add where condition to Post Model when <code>with('category')</code> is called. I should display only <code>posts.status== published</code> if <code>with('category')</code> is called.</p>

<p>I think <code>return $this-&gt;belongsTo(Category::class);</code> is where i should add my where condition, but this doesn't work: </p>

<pre><code>return $this-&gt;query()-&gt;where('posts.status', 'published')-&gt;getModel()-&gt;belongsTo(User::class)
</code></pre>

<p>How can I add where condition to all post queries if with('category') is called?
I know Laravel query scopes, but i think there is a simpler way we can use. (perhaps on <code>$this-&gt;belongsTo(Category::class)</code>)</p>