mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2025-01-31 03:16:42 +01:00
#20: Fixed an n+1 query.
This commit is contained in:
parent
be23475ae5
commit
77730e9fd9
1 changed files with 6 additions and 2 deletions
|
@ -46,7 +46,7 @@ class Genre extends Model
|
|||
* "Dummy" relation to facilitate eager-loading of a genre's track count.
|
||||
* This relationship should not be used directly.
|
||||
*
|
||||
* Inspired by: http://laravel.io/forum/05-03-2014-eloquent-get-count-relation?page=1#reply-6226
|
||||
* Inspired by {@link http://softonsofa.com/tweaking-eloquent-relations-how-to-get-hasmany-relation-count-efficiently/}
|
||||
*
|
||||
* @return Relation
|
||||
*/
|
||||
|
@ -62,7 +62,11 @@ class Genre extends Model
|
|||
* @return int
|
||||
*/
|
||||
public function getTrackCountAttribute() {
|
||||
return $this->trackCountRelation()->count();
|
||||
if (!$this->relationLoaded('trackCountRelation')) {
|
||||
$this->load('trackCountRelation');
|
||||
}
|
||||
|
||||
return $this->trackCountRelation ? $this->trackCountRelation->track_count : 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue