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