Comment.php 1008 B

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