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

Размер файла: 2.37Kb
  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Models;
  6.  
  7. use App\Traits\UploadTrait;
  8. use Exception;
  9. use Illuminate\Database\Eloquent\Collection;
  10. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  11. use Illuminate\Database\Eloquent\Relations\MorphMany;
  12. use Illuminate\Database\Eloquent\Relations\MorphOne;
  13.  
  14. /**
  15. * Class Post
  16. *
  17. * @property int id
  18. * @property int topic_id
  19. * @property int user_id
  20. * @property string text
  21. * @property int rating
  22. * @property int created_at
  23. * @property string ip
  24. * @property string brow
  25. * @property int edit_user_id
  26. * @property int updated_at
  27. * @property Collection files
  28. */
  29. class Post extends BaseModel
  30. {
  31. use UploadTrait;
  32.  
  33. /**
  34. * Indicates if the model should be timestamped.
  35. *
  36. * @var bool
  37. */
  38. public $timestamps = false;
  39.  
  40. /**
  41. * The attributes that aren't mass assignable.
  42. *
  43. * @var array
  44. */
  45. protected $guarded = [];
  46.  
  47. /**
  48. * Директория загрузки файлов
  49. *
  50. * @var string
  51. */
  52. public $uploadPath = '/uploads/forums';
  53.  
  54. /**
  55. * Morph name
  56. *
  57. * @var string
  58. */
  59. public static $morphName = 'posts';
  60.  
  61. /**
  62. * Возвращает связь пользователей
  63. *
  64. * @return BelongsTo
  65. */
  66. public function editUser(): BelongsTo
  67. {
  68. return $this->belongsTo(User::class, 'edit_user_id')->withDefault();
  69. }
  70.  
  71. /**
  72. * Возвращает топик
  73. *
  74. * @return BelongsTo
  75. */
  76. public function topic(): BelongsTo
  77. {
  78. return $this->belongsTo(Topic::class, 'topic_id')->withDefault();
  79. }
  80.  
  81. /**
  82. * Возвращает загруженные файлы
  83. *
  84. * @return MorphMany
  85. */
  86. public function files(): MorphMany
  87. {
  88. return $this->morphMany(File::class, 'relate');
  89. }
  90.  
  91. /**
  92. * Возвращает связь с голосованием
  93. *
  94. * @return morphOne
  95. */
  96. public function polling(): morphOne
  97. {
  98. return $this->morphOne(Polling::class, 'relate')->where('user_id', getUser('id'));
  99. }
  100.  
  101. /**
  102. * Удаление поста и загруженных файлов
  103. *
  104. * @return bool|null
  105. * @throws Exception
  106. */
  107. public function delete(): ?bool
  108. {
  109. $this->files->each(static function (File $file) {
  110. $file->delete();
  111. });
  112.  
  113. return parent::delete();
  114. }
  115. }