feat: user rating and comment

This commit is contained in:
2026-05-20 15:52:28 +03:30
parent 02448db346
commit d0d48866f0
7 changed files with 171 additions and 19 deletions
+46 -2
View File
@@ -10,11 +10,55 @@ public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
// Get user's specific comment
public function userComment()
{
if (!auth()->check()) return null;
return $this->comments()
->where('user_id', auth()->id())
->first();
}
// Check if user has commented
public function getHasUserCommentedAttribute()
{
return !is_null($this->userComment());
}
// Get user's comment content
public function getUserCommentAttribute()
{
$comment = $this->userComment();
return $comment ? $comment->content : null;
}
// Get user's comment id
public function getUserCommentIdAttribute()
{
$comment = $this->userComment();
return $comment ? $comment->id : null;
}
// Get latest comments with user info
public function getLatestCommentsAttribute()
{
return $this->comments()->latest()->limit(5)->get();
return $this->comments()
->with('user')
->latest()
->limit(10)
->get();
}
// Get paginated comments
public function getPaginatedComments($perPage = 15)
{
return $this->comments()
->with('user')
->latest()
->paginate($perPage);
}
public function getCommentsCountAttribute()
{
+25
View File
@@ -24,9 +24,34 @@ public function getUserRatingAttribute()
->where('user_id', auth()->id())
->value('stars');
}
// Get user's rating object
public function userRating()
{
if (!auth()->check()) return null;
return $this->ratings()
->where('user_id', auth()->id())
->first();
}
// Check if user has rated
public function getHasUserRatedAttribute()
{
return !is_null($this->userRating());
}
public function getRatingsCountAttribute()
{
return $this->ratings()->count();
}
// Get rating distribution
public function getRatingDistributionAttribute()
{
$distribution = [];
for ($i = 1; $i <= 5; $i++) {
$distribution[$i] = $this->ratings()->where('stars', $i)->count();
}
return $distribution;
}
}