Article.php 1007 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Model;
  4. use Illuminate\Database\Eloquent\SoftDeletes;
  5. class Article extends Model
  6. {
  7. use SoftDeletes;
  8. // Приведение к datetime нужно для использования метода isPast
  9. protected $casts = [
  10. "publish_at" => "datetime",
  11. "unpublish_at" => "datetime"
  12. ];
  13. const STATUS_DRAFT = 0;
  14. const STATUS_PENDING = 1;
  15. const STATUS_PUBLISHED = 2;
  16. const STATUS_ARCHIVE = 3;
  17. function scopePending($query) {
  18. $query->where("status", static::STATUS_PENDING);
  19. }
  20. function scopePublished($query) {
  21. $query->where("status", static::STATUS_PUBLISHED);
  22. }
  23. function comments() {
  24. return $this->morphMany(Comment::class, "commentable");
  25. }
  26. function publish() {
  27. $this->status = static::STATUS_PUBLISHED;
  28. $this->save();
  29. }
  30. function unpublish() {
  31. $this->status = static::STATUS_ARCHIVE;
  32. $this->save();
  33. }
  34. // Для страницы модерации
  35. function link() {
  36. return "/article/$this->id";
  37. }
  38. }