feat: add rating and comement

This commit is contained in:
2026-05-20 12:19:10 +03:30
parent e446de79fd
commit 6f5224a5d9
11 changed files with 547 additions and 32 deletions
+37 -2
View File
@@ -7,9 +7,13 @@
use Illuminate\Database\Eloquent\Relations\BelongsTo; // ← Add this
use Illuminate\Database\Eloquent\Relations\BelongsToMany; // ← Add this
use Illuminate\Database\Eloquent\Relations\HasMany; // ← Add this
use Illuminate\Database\Eloquent\Relations\MorphMany; // ← Add this
use App\Traits\HasRatings;
use App\Traits\HasComments;
class Music extends Model
{
use HasFactory;
use HasFactory, HasRatings, HasComments;
protected $table = 'music';
@@ -39,7 +43,38 @@ public function tags(): BelongsToMany
return $this->belongsToMany(Tag::class, 'music_tags');
}
protected $appends = ['url', 'image_url'];
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()
{