Fixed an n+1 query.

This commit is contained in:
Peter Deltchev 2016-02-15 07:51:26 -08:00
parent 6c83936ce6
commit 5314f5accb

View file

@ -33,8 +33,10 @@ class TaxonomiesController extends ApiControllerBase
{
return \Response::json([
'licenses' => License::all()->toArray(),
'genres' => Genre::select('genres.*',
DB::raw('(SELECT COUNT(id) FROM tracks WHERE tracks.genre_id = genres.id AND tracks.published_at IS NOT NULL) AS track_count'))->orderBy('name')->get()->toArray(),
'genres' => Genre::with('trackCountRelation')
->orderBy('name')
->get()
->toArray(),
'track_types' => TrackType::select('track_types.*',
DB::raw('(SELECT COUNT(id) FROM tracks WHERE tracks.track_type_id = track_types.id AND tracks.published_at IS NOT NULL) AS track_count'))
->where('id', '!=', TrackType::UNCLASSIFIED_TRACK)