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
+6 -25
View File
@@ -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);
}
}