Просмотр файла app/Models/News.php

Размер файла: 2.26Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use App\Traits\UploadTrait;
  8. use Illuminate\Database\Eloquent\Relations\MorphMany;
  9. use Illuminate\Database\Eloquent\Relations\MorphOne;
  10. use Illuminate\Support\HtmlString;
  11.  
  12. /**
  13. * Class News
  14. *
  15. * @property int id
  16. * @property string title
  17. * @property string text
  18. * @property int user_id
  19. * @property string image
  20. * @property int created_at
  21. * @property int count_comments
  22. * @property int closed
  23. * @property int top
  24. */
  25. class News extends BaseModel
  26. {
  27. use UploadTrait;
  28.  
  29. /**
  30. * Indicates if the model should be timestamped.
  31. *
  32. * @var bool
  33. */
  34. public $timestamps = false;
  35.  
  36. /**
  37. * The attributes that aren't mass assignable.
  38. *
  39. * @var array
  40. */
  41. protected $guarded = [];
  42.  
  43. /**
  44. * Директория загрузки файлов
  45. *
  46. * @var string
  47. */
  48. public $uploadPath = '/uploads/news';
  49.  
  50. /**
  51. * Morph name
  52. *
  53. * @var string
  54. */
  55. public static $morphName = 'news';
  56.  
  57. /**
  58. * Возвращает комментарии новостей
  59. */
  60. public function comments(): MorphMany
  61. {
  62. return $this->morphMany(Comment::class, 'relate')->with('relate');
  63. }
  64.  
  65. /**
  66. * Возвращает связь с голосованием
  67. *
  68. * @return morphOne
  69. */
  70. public function polling(): morphOne
  71. {
  72. return $this->morphOne(Polling::class, 'relate')->where('user_id', getUser('id'));
  73. }
  74.  
  75. /**
  76. * Возвращает иконку в зависимости от статуса
  77. *
  78. * @return string иконка новостей
  79. */
  80. public function getIcon(): string
  81. {
  82. if ($this->closed) {
  83. $icon = 'fa-lock';
  84. } else {
  85. $icon = 'fa-unlock';
  86. }
  87.  
  88. return $icon;
  89. }
  90.  
  91. /**
  92. * Возвращает сокращенный текст новости
  93. *
  94. * @return HtmlString
  95. */
  96. public function shortText(): HtmlString
  97. {
  98. $more = null;
  99.  
  100. if (str_contains($this->text, '[cut]')) {
  101. $this->text = current(explode('[cut]', $this->text));
  102. $more = view('app/_more', ['link' => '/news/' . $this->id]);
  103. }
  104.  
  105. return new HtmlString(bbCode($this->text) . $more);
  106. }
  107. }