23 lines
439 B
PHP
23 lines
439 B
PHP
<?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();
|
|
}
|
|
} |