diff --git a/app/Http/ViewModels/Home/HomeViewModel.php b/app/Http/ViewModels/Home/HomeViewModel.php index 6a31963..adaf782 100644 --- a/app/Http/ViewModels/Home/HomeViewModel.php +++ b/app/Http/ViewModels/Home/HomeViewModel.php @@ -36,9 +36,12 @@ public static function twentyMostPopularNames(): array ->get() ->map(fn (Name $name) => NameViewModel::summary($name)); - $randomNames = Name::where('name', '!=', '_PRENOMS_RARES') + $randomIds = Name::select('id') + ->where('name', '!=', '_PRENOMS_RARES') ->inRandomOrder() ->take(10) + ->get(); + $randomNames = Name::whereIn('id', $randomIds) ->get() ->map(fn (Name $name) => NameViewModel::summary($name)); @@ -53,10 +56,13 @@ public static function twentyMostPopularNames(): array public static function nameSpotlight(): array { $name = Cache::remember('name-of-the-day', 86400, function () { - return Name::where('total', '>', 10000) - ->select('id', 'name', 'origins') + $id = Name::select('id') + ->where('total', '>', 10000) ->inRandomOrder() ->first(); + return Name::whereIn('id', $id) + ->select('id', 'name', 'origins') + ->first(); }); return [ @@ -86,10 +92,14 @@ public static function serverStats(): array */ public static function adminLists(): Collection { - return NameList::where('is_public', true) + $ids = NameList::select('id') + ->where('is_public', true) + ->inRandomOrder() + ->get(); + + return NameList::whereIn('id', $ids) ->withCount('names') ->with('names') - ->inRandomOrder() ->get() ->map(fn (NameList $list) => [ 'id' => $list->id, diff --git a/app/Http/ViewModels/Names/NameViewModel.php b/app/Http/ViewModels/Names/NameViewModel.php index 210c81f..a929698 100644 --- a/app/Http/ViewModels/Names/NameViewModel.php +++ b/app/Http/ViewModels/Names/NameViewModel.php @@ -117,11 +117,15 @@ public static function jsonLdSchema(Name $name): array public static function relatedNames(Name $name): Collection { - return Name::where('name', '!=', '_PRENOMS_RARES') + $ids = Name::select('id') + ->where('name', '!=', '_PRENOMS_RARES') ->where('id', '!=', $name->id) ->where('gender', $name->gender) ->inRandomOrder() ->take(10) + ->get(); + + return Name::whereIn('id', $ids) ->get() ->map(fn (Name $name) => NameViewModel::summary($name)); } diff --git a/database/migrations/2024_02_17_091516_name_index.php b/database/migrations/2024_02_17_091516_name_index.php new file mode 100644 index 0000000..d07558f --- /dev/null +++ b/database/migrations/2024_02_17_091516_name_index.php @@ -0,0 +1,29 @@ +index(['gender', 'name', 'unisex']); + $table->index(['total']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('names', function (Blueprint $table) { + $table->dropIndex('names_gender_name_index'); + }); + } +};