feat: add ratings
This commit is contained in:
@@ -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
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user