Laravel 5.2 query scope on polymorphic relationship
<p>I have a one to many polymorphic relationship exactly like described <a href="https://laravel.com/docs/5.1/eloquent-relationships#polymorphic-relations" rel="noreferrer">in documentation</a>, except I have votes instead of photos and an integer vote column in the votes table. Like that:</p>

<pre><code>-votes
--id
--vote
--voteable_id
--voteable_type
</code></pre>

<p>The Vote model has the voteable method:</p>

<pre><code>public function voteable()
{
    return $this-&gt;morphTo();
}
</code></pre>

<p>But for models that I want to have votes attached to (Page in this example) I have created a VoteableTrait, which has the votes() morphMany method and a scope I'm trying to get to work. The scope would add the sum of all votes that belong to a certain model and order the parent models by the sum. Scope as I've tried:</p>

<pre><code>public function scopeWithVotesTrait($query)
{
    return $query-&gt;with(['votes' =&gt; function($q){
        $q-&gt;select(\DB::raw("SUM(vote) AS votes"), 'voteable_id')-&gt;first();
    }]);
}
</code></pre>

<p>But with this I cannot orderBy as Laravel performs 2 different queries, one for parent model and one for votes it seems. The result of Page::withVotesTrait()->first() puts votes in an array:</p>

<pre><code>{
"id": 1,
    "votes": [
    {
        "votes": "-1",
        "voteable_id": 1
    }
    ]
}
</code></pre>

<p>The other idea I had was to perform a join instead of ->with() like that:</p>

<pre><code>public function scopeWithVotesTrait($query, $model, $table)
{
    return $query-&gt;join('votes', function($join) use($model, $table)
    {
        $join-&gt;on('votes.voteable_id', '=', $table . '.id')
            -&gt;where('votes.voteable_type', '=', $model);
    })-&gt;select('*', \DB::raw('sum(vote) as votes'))
    -&gt;orderBy('votes')-&gt;groupBy($table . '.id');
}
</code></pre>

<p>But if I have to manually put in $model ('App\ParentModel') and $table ('parent_models') every time I use the scope, that kind of beats the purpose of putting it in a trait.</p>

<p>So is there either a way to efficiently order by the sum of votes in my first example of scope? Or is there any static method you could call from Eloquent Model that would return the model name and one for model table to make the second example work?</p>

<p>Any other ideas how to make a scope that would attach sum of all votes to the parent model and order by it?</p>