1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6declare(strict_types=1);
7
8namespace App\Models;
9
10use Illuminate\Contracts\Database\Eloquent\Builder;
11
12/**
13 * @property-read Beatmap $beatmap
14 * @property int $beatmap_id
15 * @property \Carbon\Carbon $created_at
16 * @property int $tag_id
17 * @property \Carbon\Carbon $updated_at
18 * @property-read User $user
19 * @property int $user_id
20 */
21class BeatmapTag extends Model
22{
23 public $incrementing = false;
24
25 protected $primaryKey = ':composite';
26 protected $primaryKeys = ['beatmap_id', 'tag_id', 'user_id'];
27
28 public function scopeTopTagIds(Builder $query)
29 {
30 return $query->whereHas('user', fn ($userQuery) => $userQuery->default())
31 ->groupBy('tag_id')
32 ->select('tag_id')
33 ->selectRaw('COUNT(*) as count')
34 ->orderBy('count', 'desc')
35 ->orderBy('tag_id', 'asc');
36 }
37
38 public function beatmap()
39 {
40 return $this->belongsTo(Beatmap::class, 'beatmap_id');
41 }
42
43 public function user()
44 {
45 return $this->belongsTo(User::class, 'user_id');
46 }
47}