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

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