Comment.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Factories\HasFactory;
  4. use Illuminate\Database\Eloquent\Model;
  5. use App\Events\CommentCreated;
  6. use App\Events\CommentApproved;
  7. class Comment extends Model
  8. {
  9. use HasFactory;
  10. protected $fillable = [
  11. 'post_id',
  12. 'author_name',
  13. 'author_email',
  14. 'content',
  15. 'status',
  16. ];
  17. protected $dispatchesEvents = [
  18. 'created' => CommentCreated::class,
  19. ];
  20. // Связь с постом
  21. public function post()
  22. {
  23. return $this->belongsTo(Post::class);
  24. }
  25. // Только одобренные комментарии
  26. public function scopeApproved($query)
  27. {
  28. return $query->where('status', 'approved');
  29. }
  30. // Ожидающие модерации
  31. public function scopePending($query)
  32. {
  33. return $query->where('status', 'pending');
  34. }
  35. // Одобрить комментарий
  36. public function approve()
  37. {
  38. $this->update(['status' => 'approved']);
  39. event(new CommentApproved($this));
  40. }
  41. // Отклонить комментарий
  42. public function reject()
  43. {
  44. $this->update(['status' => 'rejected']);
  45. }
  46. }