| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- <?php
- namespace App\Models;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use App\Events\CommentCreated;
- use App\Events\CommentApproved;
- class Comment extends Model
- {
- use HasFactory;
- protected $fillable = [
- 'post_id',
- 'author_name',
- 'author_email',
- 'content',
- 'status',
- ];
- protected $dispatchesEvents = [
- 'created' => CommentCreated::class,
- ];
- // Связь с постом
- public function post()
- {
- return $this->belongsTo(Post::class);
- }
- // Только одобренные комментарии
- public function scopeApproved($query)
- {
- return $query->where('status', 'approved');
- }
- // Ожидающие модерации
- public function scopePending($query)
- {
- return $query->where('status', 'pending');
- }
- // Одобрить комментарий
- public function approve()
- {
- $this->update(['status' => 'approved']);
- event(new CommentApproved($this));
- }
- // Отклонить комментарий
- public function reject()
- {
- $this->update(['status' => 'rejected']);
- }
- }
|