Comment.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. use App\Events\CommentAdded;
  6. class Comment extends Model
  7. {
  8. use SoftDeletes;
  9. const STATUS_NEW = 0; // Ждут автоматической проверки
  10. const STATUS_PENDING = 1; // Ждут проверки модератором
  11. const STATUS_PUBLISHED = 2; // Опубликованы
  12. const STATUS_REJECTED = 3; // Не прошли проверку
  13. // Наличие fillable требуется только при использовании mass assignment, т.е. создание записи из ассоциативного массива
  14. protected $fillable = [
  15. "name",
  16. "email",
  17. "content",
  18. "status"
  19. ];
  20. protected $dispatchesEvents = [
  21. "created" => CommentAdded::class
  22. ];
  23. function commentable() {
  24. return $this->morphTo();
  25. }
  26. function scopePending($query) {
  27. $query->where("status", static::STATUS_PENDING);
  28. }
  29. function scopePublished($query) {
  30. $query->where("status", static::STATUS_PUBLISHED);
  31. }
  32. function scopeRecent($query) {
  33. $query->orderBy("created_at", "desc");
  34. }
  35. function makePending() {
  36. $this->status = static::STATUS_PENDING;
  37. $this->save();
  38. }
  39. function allow() {
  40. $this->status = static::STATUS_PUBLISHED;
  41. $this->save();
  42. }
  43. function reject() {
  44. $this->status = static::STATUS_REJECTED;
  45. $this->save();
  46. }
  47. }