whereNot('albums.id', Album::UNKNOWN_ID); } public function withMeta(User $user): static { return $this->with('artist') ->leftJoin('songs', 'albums.id', '=', 'songs.album_id') ->leftJoin('interactions', static function (JoinClause $join) use ($user): void { $join->on('songs.id', '=', 'interactions.song_id')->where('interactions.user_id', $user->id); }) ->groupBy('albums.id') ->select( 'albums.*', DB::raw('CAST(SUM(interactions.play_count) AS UNSIGNED) AS play_count') ) ->withCount('songs AS song_count') ->withSum('songs AS length', 'length'); } }