I've got following query which beats the crap out of my database, because it makes a a single query for every relation:
$projects = $this->project->all()->sortBy(function ($item) {
return $item->votes()->count();
}, SORT_REGULAR, true)->take(30);
I tried many different ways:
$projects = $this->project->with('Vote')->all()->sortBy(...)->take(30);
Call to undefined method Illuminate\Database\Query\Builder::all()
$projects = $this->project->with('Vote')->get()->sortBy(...)->take(30);
Call to undefined method Illuminate\Database\Query\Builder::Vote()
...
In the template I'm looping over the projects and use $project->votes()->count()
What is the correct way to do the eager loading?