refactor
This commit is contained in:
@@ -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