feat: add rating and comement #2
@@ -321,7 +321,7 @@ public function filters(Request $request)
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$media = Media::with(['image', 'category', 'myNote', 'tags' ,'comments'])
|
||||
$media = Media::with(['image', 'category', 'myNote', 'tags'])
|
||||
->where('id', $id)
|
||||
->where(function($query) {
|
||||
$query->where('visibility', 'public')
|
||||
@@ -348,7 +348,7 @@ public function show($id)
|
||||
'is_premium' => $media->is_premium,
|
||||
'comments' => $media->comments,
|
||||
'average_rating' => $media->average_rating,
|
||||
'your_rating' => $media->user_rating,
|
||||
'user_rating' => $media->user_rating,
|
||||
'comments_count' => $media->comments()->count(),
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -45,37 +45,6 @@ public function tags(): BelongsToMany
|
||||
|
||||
protected $appends = ['url', 'image_url' , 'average_rating', 'user_rating', 'comments_count'];
|
||||
|
||||
// New rating and comment relationships
|
||||
public function ratings(): MorphMany
|
||||
{
|
||||
return $this->morphMany(Rating::class, 'rateable');
|
||||
}
|
||||
|
||||
public function comments(): MorphMany
|
||||
{
|
||||
return $this->morphMany(Comment::class, 'commentable');
|
||||
}
|
||||
|
||||
// Accessors for ratings
|
||||
public function getAverageRatingAttribute(): float
|
||||
{
|
||||
return round($this->ratings()->avg('stars'), 1);
|
||||
}
|
||||
|
||||
public function getUserRatingAttribute(): ?int
|
||||
{
|
||||
if (!auth()->check()) return null;
|
||||
|
||||
return $this->ratings()
|
||||
->where('user_id', auth()->id())
|
||||
->value('stars');
|
||||
}
|
||||
|
||||
public function getCommentsCountAttribute(): int
|
||||
{
|
||||
return $this->comments()->count();
|
||||
}
|
||||
// Accessor for full URL
|
||||
public function getUrlAttribute()
|
||||
{
|
||||
return asset('storage/' . $this->file_path);
|
||||
|
||||
Reference in New Issue
Block a user