Skip to content

Commit d36579c

Browse files
authored
Merge pull request #230 from Muetze42/development
chore(stats): replace raw DB queries with Eloquent relationships in top stats methods
2 parents fbf298c + da0cba4 commit d36579c

File tree

3 files changed

+21
-27
lines changed

3 files changed

+21
-27
lines changed

app/Livewire/Portal/Stats/RecipeStats.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use App\Livewire\AbstractComponent;
66
use App\Models\Country;
7+
use App\Models\Cuisine;
8+
use App\Models\Ingredient;
79
use App\Models\Recipe;
10+
use App\Models\Tag;
811
use App\Services\Portal\StatisticsService;
912
use Illuminate\Contracts\View\View;
1013
use Illuminate\Support\Collection;
@@ -101,7 +104,7 @@ public function recipeQuality(): array
101104
/**
102105
* Get top ingredients by recipe count.
103106
*
104-
* @return Collection<int, stdClass>
107+
* @return Collection<int, Ingredient>
105108
*/
106109
#[Computed]
107110
public function topIngredients(): Collection
@@ -112,7 +115,7 @@ public function topIngredients(): Collection
112115
/**
113116
* Get top tags by recipe count.
114117
*
115-
* @return Collection<int, stdClass>
118+
* @return Collection<int, Tag>
116119
*/
117120
#[Computed]
118121
public function topTags(): Collection
@@ -123,7 +126,7 @@ public function topTags(): Collection
123126
/**
124127
* Get top cuisines by recipe count.
125128
*
126-
* @return Collection<int, stdClass>
129+
* @return Collection<int, Cuisine>
127130
*/
128131
#[Computed]
129132
public function topCuisines(): Collection

app/Services/Portal/StatisticsService.php

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,13 @@ public function recipeQuality(): array
168168
/**
169169
* Get top ingredients by recipe count.
170170
*
171-
* @return Collection<int, stdClass>
171+
* @return Collection<int, Ingredient>
172172
*/
173173
public function topIngredients(): Collection
174174
{
175-
/** @var Collection<int, stdClass> */
176-
return Cache::remember('portal_top_ingredients', $this->cacheTtl, static fn (): Collection => DB::table('ingredients')
177-
->join('ingredient_recipe', 'ingredients.id', '=', 'ingredient_recipe.ingredient_id')
178-
->join('countries', 'ingredients.country_id', '=', 'countries.id')
179-
->select('ingredients.name', 'countries.code as country_code', 'countries.locales as country_locales', DB::raw('COUNT(ingredient_recipe.recipe_id) as recipes_count'))
180-
->groupBy('ingredients.id', 'ingredients.name', 'countries.code', 'countries.locales')
175+
/** @var Collection<int, Ingredient> */
176+
return Cache::remember('portal_top_ingredients', $this->cacheTtl, static fn (): Collection => Ingredient::withCount('recipes')
177+
->with('country:id,code')
181178
->orderByDesc('recipes_count')
182179
->limit(10)
183180
->get());
@@ -186,16 +183,13 @@ public function topIngredients(): Collection
186183
/**
187184
* Get top tags by recipe count.
188185
*
189-
* @return Collection<int, stdClass>
186+
* @return Collection<int, Tag>
190187
*/
191188
public function topTags(): Collection
192189
{
193-
/** @var Collection<int, stdClass> */
194-
return Cache::remember('portal_top_tags', $this->cacheTtl, static fn (): Collection => DB::table('tags')
195-
->join('recipe_tag', 'tags.id', '=', 'recipe_tag.tag_id')
196-
->join('countries', 'tags.country_id', '=', 'countries.id')
197-
->select('tags.name', 'countries.code as country_code', 'countries.locales as country_locales', DB::raw('COUNT(recipe_tag.recipe_id) as recipes_count'))
198-
->groupBy('tags.id', 'tags.name', 'countries.code', 'countries.locales')
190+
/** @var Collection<int, Tag> */
191+
return Cache::remember('portal_top_tags', $this->cacheTtl, static fn (): Collection => Tag::withCount('recipes')
192+
->with('country:id,code')
199193
->orderByDesc('recipes_count')
200194
->limit(10)
201195
->get());
@@ -204,16 +198,13 @@ public function topTags(): Collection
204198
/**
205199
* Get top cuisines by recipe count.
206200
*
207-
* @return Collection<int, stdClass>
201+
* @return Collection<int, Cuisine>
208202
*/
209203
public function topCuisines(): Collection
210204
{
211-
/** @var Collection<int, stdClass> */
212-
return Cache::remember('portal_top_cuisines', $this->cacheTtl, static fn (): Collection => DB::table('cuisines')
213-
->join('cuisine_recipe', 'cuisines.id', '=', 'cuisine_recipe.cuisine_id')
214-
->join('countries', 'cuisines.country_id', '=', 'countries.id')
215-
->select('cuisines.name', 'countries.code as country_code', 'countries.locales as country_locales', DB::raw('COUNT(cuisine_recipe.recipe_id) as recipes_count'))
216-
->groupBy('cuisines.id', 'cuisines.name', 'countries.code', 'countries.locales')
205+
/** @var Collection<int, Cuisine> */
206+
return Cache::remember('portal_top_cuisines', $this->cacheTtl, static fn (): Collection => Cuisine::withCount('recipes')
207+
->with('country:id,code')
217208
->orderByDesc('recipes_count')
218209
->limit(10)
219210
->get());

resources/views/portal/livewire/stats/recipe-stats.blade.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@
210210
@foreach($this->topIngredients as $ingredient)
211211
<flux:table.row wire:key="ingredient-{{ $loop->index }}">
212212
<flux:table.cell class="truncate max-w-48">
213-
<x-flag :code="$ingredient->country_code" :title="$ingredient->country_code" />
213+
<x-flag :code="$ingredient->country->code" :title="$ingredient->country->code" />
214214
{{ $ingredient->name }}
215215
</flux:table.cell>
216216
<flux:table.cell align="end" class="tabular-nums">{{ Number::format($ingredient->recipes_count) }}</flux:table.cell>
@@ -231,7 +231,7 @@
231231
@foreach($this->topTags as $tag)
232232
<flux:table.row wire:key="tag-{{ $loop->index }}">
233233
<flux:table.cell class="truncate max-w-48">
234-
<x-flag :code="$tag->country_code" :title="$tag->country_code" />
234+
<x-flag :code="$tag->country->code" :title="$tag->country->code" />
235235
{{ $tag->name }}
236236
</flux:table.cell>
237237
<flux:table.cell align="end" class="tabular-nums">{{ Number::format($tag->recipes_count) }}</flux:table.cell>
@@ -252,7 +252,7 @@
252252
@foreach($this->topCuisines as $cuisine)
253253
<flux:table.row wire:key="cuisine-{{ $loop->index }}">
254254
<flux:table.cell class="truncate max-w-48">
255-
<x-flag :code="$cuisine->country_code" :title="$cuisine->country_code" />
255+
<x-flag :code="$cuisine->country->code" :title="$cuisine->country->code" />
256256
{{ $cuisine->name }}
257257
</flux:table.cell>
258258
<flux:table.cell align="end" class="tabular-nums">{{ Number::format($cuisine->recipes_count) }}</flux:table.cell>

0 commit comments

Comments
 (0)