feat: add ratings

This commit is contained in:
2026-02-25 19:37:53 +03:30
parent 4d0823ef86
commit 676522d70d
7 changed files with 270 additions and 5 deletions
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = [
'user_id',
'media_id',
'content',
];
public function user()
{
return $this->belongsTo(User::class);
}
public function media()
{
return $this->belongsTo(Media::class);
}
}
+27 -2
View File
@@ -19,7 +19,10 @@ class Media extends Model
'visibility',
'is_premium'
];
protected $appends = [
'average_rating',
'user_rating',
];
public function image()
{
return $this->belongsTo(Image::class);
@@ -65,5 +68,27 @@ 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);
}
}
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Rating extends Model
{
protected $fillable = [
'user_id',
'media_id',
'stars',
];
public function media()
{
return $this->belongsTo(Media::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
}