Comment.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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_PENDING = 0;
  10. const STATUS_PUBLISHED = 1;
  11. const STATUS_REJECTED = 2;
  12. // Наличие fillable требуется только при использовании mass assignment, т.е. создание записи из ассоциативного массива
  13. protected $fillable = [
  14. "name",
  15. "email",
  16. "content",
  17. "status"
  18. ];
  19. protected $dispatchesEvents = [
  20. "created" => CommentAdded::class
  21. ];
  22. function commentable() {
  23. return $this->morphTo();
  24. }
  25. function scopePending($query) {
  26. $query->where("status", static::STATUS_PENDING);
  27. }
  28. function scopePublished($query) {
  29. $query->where("status", static::STATUS_PUBLISHED);
  30. }
  31. function scopeRecent($query) {
  32. $query->orderBy("created_at", "desc");
  33. }
  34. function allow() {
  35. $this->status = static::STATUS_PUBLISHED;
  36. $this->save();
  37. }
  38. function reject() {
  39. $this->status = static::STATUS_REJECTED;
  40. $this->save();
  41. }
  42. }