Skip to content

Commit 5eb03f3

Browse files
committed
fix: streamline user ID retrieval in resource classes
1 parent b1cf42b commit 5eb03f3

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

src/Http/Resources/Comment/CommentResource.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CSlant\Blog\Core\Models\Comment;
77
use Illuminate\Http\Request;
88
use Illuminate\Http\Resources\Json\JsonResource;
9+
use Illuminate\Support\Facades\Auth;
910

1011
/**
1112
* @mixin Comment
@@ -19,13 +20,17 @@ class CommentResource extends JsonResource
1920
*/
2021
public function toArray(Request $request): array
2122
{
23+
$userId = Auth::user() ? Auth::user()->id : 0;
24+
2225
/** @var Comment $this */
2326
return [
2427
'id' => $this->id,
2528
'website' => $this->website,
2629
'content' => $this->content,
2730
'status' => $this->status,
2831
'author' => AuthorResource::make($this->author),
32+
'likes_count' => $this->likesCountDigital(),
33+
'is_liked' => $this->isLikedBy($userId),
2934
];
3035
}
3136
}

src/Http/Resources/Post/ListPostResource.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ class ListPostResource extends JsonResource
2424
*/
2525
public function toArray($request): array
2626
{
27-
$userId = 0;
28-
if (Auth::user()) {
29-
$userId = Auth::user()->id;
30-
}
27+
$userId = Auth::user() ? Auth::user()->id : 0;
3128

3229
/** @var Post $this */
3330
return [

src/Http/Resources/Post/PostResource.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ public function toArray($request): array
2929
->orderBy((string) $request->get('order_by', 'created_at'), (string) $request->get('order', 'DESC'))
3030
->paginate($request->integer('per_page', 10));
3131

32-
$userId = 0;
33-
if (Auth::user()) {
34-
$userId = Auth::user()->id;
35-
}
32+
$userId = Auth::user() ? Auth::user()->id : 0;
3633

3734
return [
3835
'id' => $this->id,

0 commit comments

Comments
 (0)