feat: user rating and comment
This commit is contained in:
@@ -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()
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user