How to filter results based on relationships in query scope?
<p>I am writing an application where I have two models namely <code>AngencyProfile</code>and <code>Ad</code>. I have created a polymorphic relationship of the both models. </p>

<p>The structure of the <code>ads table</code> is as follows:</p>

<p><a href="https://i.stack.imgur.com/ULtCZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ULtCZ.png" alt="Schema"></a></p>

<p>I need to write query scope for <code>AngencyProfile</code> to retrieve the agency profiles based on criteria, lets say <code>where remaining_budget &gt;= cpc</code>. </p>

<p>How can I achieve this? Or is it possible?</p>

<p>The relationship and query scope that I am writing now is as follows:</p>

<pre><code>public function ads() {
    return $this-&gt;morphMany(Ad::class, 'advertisable');
}
public function scopeAdvertisable($query)
{
    return $query-&gt;ads()-&gt;where('remaining_budget', '&gt;=', 'cpc)-&gt;get();
}
</code></pre>

<p>This is throwing an exception. </p>

<blockquote>
  <p>Call to undefined method Illuminate\Database\Query\Builder::ads()</p>
</blockquote>

<p>Your help will be highly appreciated. </p>