feat: user rating and comment
This commit is contained in:
@@ -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