Added a bunch of eager-loading.

This commit is contained in:
Peter Deltchev 2016-05-28 12:29:06 -07:00
parent fc4ae856f8
commit 27f19f9dc7
5 changed files with 37 additions and 9 deletions

View file

@ -61,7 +61,10 @@ class AlbumsController extends ApiControllerBase
'tracks.cover', 'tracks.cover',
'tracks.genre', 'tracks.genre',
'tracks.user', 'tracks.user',
'tracks.user.avatar',
'tracks.trackFiles',
'user', 'user',
'user.avatar',
'comments', 'comments',
'comments.user' 'comments.user'
]) ])
@ -146,7 +149,10 @@ class AlbumsController extends ApiControllerBase
{ {
$this->authorize('get-albums', $user); $this->authorize('get-albums', $user);
$query = Album::summary()->where('user_id', $user->id)->orderBy('created_at', 'desc')->get(); $query = Album::summary()
->with('cover', 'user.avatar')
->where('user_id', $user->id)
->orderBy('created_at', 'desc')->get();
$albums = []; $albums = [];
foreach ($query as $album) { foreach ($query as $album) {

View file

@ -48,8 +48,13 @@ class ArtistsController extends ApiControllerBase
'track.genre', 'track.genre',
'track.cover', 'track.cover',
'track.user', 'track.user',
'track.user.avatar',
'track.album',
'track.album.cover',
'track.album.user.avatar',
'album.cover', 'album.cover',
'album.user', 'album.user',
'album.user.avatar',
'track' => function ($query) { 'track' => function ($query) {
$query->userDetails(); $query->userDetails();
}, },
@ -84,8 +89,14 @@ class ArtistsController extends ApiControllerBase
App::abort(404); App::abort(404);
} }
$query = Track::summary()->published()->listed()->explicitFilter()->with('genre', 'cover', $query = Track::summary()
'user')->userDetails()->whereUserId($user->id)->whereNotNull('published_at'); ->published()
->listed()
->explicitFilter()
->with('genre', 'cover', 'user', 'user.avatar', 'album', 'album.cover')
->userDetails()
->whereUserId($user->id)
->whereNotNull('published_at');
$tracks = []; $tracks = [];
$singles = []; $singles = [];
@ -119,7 +130,7 @@ class ArtistsController extends ApiControllerBase
->userDetails() ->userDetails()
->with([ ->with([
'comments' => function ($query) { 'comments' => function ($query) {
$query->with('user'); $query->with(['user', 'user.avatar']);
} }
]) ])
->first(); ->first();
@ -131,7 +142,7 @@ class ArtistsController extends ApiControllerBase
->published() ->published()
->explicitFilter() ->explicitFilter()
->listed() ->listed()
->with('genre', 'cover', 'user') ->with('genre', 'cover', 'user', 'album', 'album.cover')
->userDetails() ->userDetails()
->whereUserId($user->id) ->whereUserId($user->id)
->whereNotNull('published_at') ->whereNotNull('published_at')

View file

@ -70,7 +70,14 @@ class PlaylistsController extends ApiControllerBase
} }
$query = Playlist::summary() $query = Playlist::summary()
->with('user', 'user.avatar', 'tracks', 'tracks.cover', 'tracks.user', 'tracks.album', 'tracks.album.user') ->with('user',
'user.avatar',
'tracks',
'tracks.cover',
'tracks.user',
'tracks.user.avatar',
'tracks.album',
'tracks.album.user')
->userDetails() ->userDetails()
->orderBy('title', 'asc') ->orderBy('title', 'asc')
->where('track_count', '>', 0) ->where('track_count', '>', 0)
@ -103,6 +110,7 @@ class PlaylistsController extends ApiControllerBase
'tracks' => function ($query) { 'tracks' => function ($query) {
$query->userDetails(); $query->userDetails();
}, },
'tracks.trackFiles',
'comments', 'comments',
'comments.user' 'comments.user'
])->userDetails()->find($id); ])->userDetails()->find($id);

View file

@ -88,7 +88,8 @@ class Album extends Model implements Searchable, Commentable, Favouritable
'users' => function($query) { 'users' => function($query) {
$query->whereUserId(Auth::user()->id); $query->whereUserId(Auth::user()->id);
} }
]); ])
->with('users.avatar');
} }
return !$query; return !$query;

View file

@ -210,7 +210,8 @@ class Track extends Model implements Searchable, Commentable, Favouritable
return self::select('tracks.id', 'title', 'user_id', 'slug', 'is_vocal', 'is_explicit', 'created_at', return self::select('tracks.id', 'title', 'user_id', 'slug', 'is_vocal', 'is_explicit', 'created_at',
'published_at', 'published_at',
'duration', 'is_downloadable', 'genre_id', 'track_type_id', 'cover_id', 'album_id', 'comment_count', 'duration', 'is_downloadable', 'genre_id', 'track_type_id', 'cover_id', 'album_id', 'comment_count',
'download_count', 'view_count', 'play_count', 'favourite_count'); 'download_count', 'view_count', 'play_count', 'favourite_count')
->with('user', 'cover', 'album');
} }
public function scopeUserDetails($query) public function scopeUserDetails($query)
@ -561,7 +562,8 @@ class Track extends Model implements Searchable, Commentable, Favouritable
*/ */
public function getFilesize($formatName) public function getFilesize($formatName)
{ {
$trackFile = $this->trackFiles()->where('format', $formatName)->first(); // $trackFile = $this->trackFiles()->where('format', $formatName)->first();
$trackFile = $this->trackFiles->where('format', $formatName)->first();
if ($trackFile) { if ($trackFile) {
return (int) $trackFile->filesize; return (int) $trackFile->filesize;