feat: add rating and comement
This commit is contained in:
+15
-2
@@ -8,7 +8,8 @@ class Comment extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'media_id',
|
||||
'commentable_id',
|
||||
'commentable_type',
|
||||
'content',
|
||||
];
|
||||
|
||||
@@ -17,8 +18,20 @@ public function user()
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function commentable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
// For backward compatibility with Media
|
||||
public function media()
|
||||
{
|
||||
return $this->belongsTo(Media::class);
|
||||
return $this->belongsTo(Media::class, 'commentable_id')->where('commentable_type', Media::class);
|
||||
}
|
||||
|
||||
// For Music
|
||||
public function music()
|
||||
{
|
||||
return $this->belongsTo(Music::class, 'commentable_id')->where('commentable_type', Music::class);
|
||||
}
|
||||
}
|
||||
|
||||
+6
-25
@@ -3,9 +3,11 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use App\Traits\HasRatings;
|
||||
use App\Traits\HasComments;
|
||||
class Media extends Model
|
||||
{
|
||||
use HasRatings, HasComments;
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'image_id',
|
||||
@@ -21,7 +23,9 @@ class Media extends Model
|
||||
];
|
||||
protected $appends = [
|
||||
'average_rating',
|
||||
'user_rating',
|
||||
'user_rating',
|
||||
'ratings_count',
|
||||
'comments_count'
|
||||
];
|
||||
public function image()
|
||||
{
|
||||
@@ -68,27 +72,4 @@ public function getIsSavedAttribute()
|
||||
->where('user_id', auth()->id())
|
||||
->exists();
|
||||
}
|
||||
public function ratings()
|
||||
{
|
||||
return $this->hasMany(Rating::class);
|
||||
}
|
||||
|
||||
public function getAverageRatingAttribute()
|
||||
{
|
||||
return round($this->ratings()->avg('stars'), 1);
|
||||
}
|
||||
|
||||
public function getUserRatingAttribute()
|
||||
{
|
||||
if (!auth()->check()) return null;
|
||||
|
||||
return $this->ratings()
|
||||
->where('user_id', auth()->id())
|
||||
->value('stars');
|
||||
}
|
||||
|
||||
public function comments()
|
||||
{
|
||||
return $this->hasMany(Comment::class);
|
||||
}
|
||||
}
|
||||
|
||||
+37
-2
@@ -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()
|
||||
{
|
||||
|
||||
+16
-3
@@ -8,17 +8,30 @@ class Rating extends Model
|
||||
{
|
||||
protected $fillable = [
|
||||
'user_id',
|
||||
'media_id',
|
||||
'rateable_id',
|
||||
'rateable_type',
|
||||
'stars',
|
||||
];
|
||||
|
||||
public function media()
|
||||
public function rateable()
|
||||
{
|
||||
return $this->belongsTo(Media::class);
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
// For backward compatibility with Media
|
||||
public function media()
|
||||
{
|
||||
return $this->belongsTo(Media::class, 'rateable_id')->where('rateable_type', Media::class);
|
||||
}
|
||||
|
||||
// For Music
|
||||
public function music()
|
||||
{
|
||||
return $this->belongsTo(Music::class, 'rateable_id')->where('rateable_type', Music::class);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user