feat: add rating and comement
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
// app/Traits/HasComments.php
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\Comment;
|
||||
|
||||
trait HasComments
|
||||
{
|
||||
public function comments()
|
||||
{
|
||||
return $this->morphMany(Comment::class, 'commentable');
|
||||
}
|
||||
|
||||
public function getLatestCommentsAttribute()
|
||||
{
|
||||
return $this->comments()->latest()->limit(5)->get();
|
||||
}
|
||||
|
||||
public function getCommentsCountAttribute()
|
||||
{
|
||||
return $this->comments()->count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
// app/Traits/HasRatings.php
|
||||
namespace App\Traits;
|
||||
|
||||
use App\Models\Rating;
|
||||
|
||||
trait HasRatings
|
||||
{
|
||||
public function ratings()
|
||||
{
|
||||
return $this->morphMany(Rating::class, 'rateable');
|
||||
}
|
||||
|
||||
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 getRatingsCountAttribute()
|
||||
{
|
||||
return $this->ratings()->count();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user