Skip to content

Commit 6c525b0

Browse files
Use eager-loaded user data for submission avatars, include user flags in featured submissions, and fix sitemap lesson fields
- Add users.is_pro, users.is_admin and users.settings to the featured submissions user select so avatar resource has required attributes - Replace manual user query in ChallengeUserCardResource with relationLoaded() check and pass the loaded user to UserAvatarResource (avoids extra query) - Update workshop lessons select for sitemap to include id, lessonable_id and lessonable_type
1 parent b9a9acb commit 6c525b0

2 files changed

Lines changed: 20 additions & 22 deletions

File tree

app/Http/Controllers/HomeController.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,10 @@ public function index()
110110
'users.id',
111111
'users.avatar_url',
112112
'users.name',
113-
'users.github_user'
113+
'users.github_user',
114+
'users.is_pro',
115+
'users.is_admin',
116+
'users.settings'
114117
);
115118
},
116119
])
@@ -193,7 +196,7 @@ protected function getSitemapItems()
193196
->withCount('lessons')
194197
->with([
195198
'lessons' => function ($query) {
196-
$query->select('workshop_id', 'slug', 'updated_at');
199+
$query->select('id', 'lessonable_id', 'lessonable_type', 'slug', 'updated_at');
197200
},
198201
])
199202
->get();

app/Http/Resources/ChallengeUserCardResource.php

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,22 @@ public function toArray(Request $request): array
1717
return [
1818
'id' => $this->id,
1919
'submission_image_url' => $this->submission_image_url,
20-
'avatar' => new UserAvatarResource(
21-
$this->user
22-
->query()
23-
->select(
24-
'avatar_url',
25-
'name',
26-
'github_user',
27-
'is_pro',
28-
'is_admin',
29-
'settings'
30-
)
31-
->find($this->user_id)
20+
'avatar' => $this->when(
21+
$this->relationLoaded('user') && $this->user,
22+
fn () => new UserAvatarResource($this->user)
3223
),
33-
'challenge' => $this->whenLoaded('challenge', [
34-
'name' => $this->challenge->name,
35-
'slug' => $this->challenge->slug,
36-
]),
37-
'user' => $this->whenLoaded('user', [
38-
'name' => $this->user->name,
39-
'github_user' => $this->user->github_user,
40-
]),
24+
'challenge' => $this->whenLoaded('challenge', function () {
25+
return [
26+
'name' => $this->challenge->name,
27+
'slug' => $this->challenge->slug,
28+
];
29+
}),
30+
'user' => $this->whenLoaded('user', function () {
31+
return [
32+
'name' => $this->user->name,
33+
'github_user' => $this->user->github_user,
34+
];
35+
}),
4136
];
4237
}
4338
}

0 commit comments

Comments
 (0)