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

Размер файла: 2.07Kb
  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\MorphMany;
  11. use Illuminate\Database\Eloquent\Relations\MorphOne;
  12.  
  13. /**
  14. * Class Photo
  15. *
  16. * @property int id
  17. * @property int user_id
  18. * @property string title
  19. * @property string text
  20. * @property int created_at
  21. * @property int rating
  22. * @property int closed
  23. * @property int count_comments
  24. * @property Collection files
  25. */
  26. class Photo extends BaseModel
  27. {
  28. use UploadTrait;
  29.  
  30. /**
  31. * Indicates if the model should be timestamped.
  32. *
  33. * @var bool
  34. */
  35. public $timestamps = false;
  36.  
  37. /**
  38. * The attributes that aren't mass assignable.
  39. *
  40. * @var array
  41. */
  42. protected $guarded = [];
  43.  
  44. /**
  45. * Директория загрузки файлов
  46. *
  47. * @var string
  48. */
  49. public $uploadPath = UPLOADS . '/photos';
  50.  
  51. /**
  52. * Morph name
  53. *
  54. * @var string
  55. */
  56. public static $morphName = 'photos';
  57.  
  58. /**
  59. * Возвращает комментарии фотографий
  60. *
  61. * @return MorphMany
  62. */
  63. public function comments(): MorphMany
  64. {
  65. return $this->morphMany(Comment::class, 'relate')->with('relate');
  66. }
  67.  
  68. /**
  69. * Возвращает загруженные файлы
  70. *
  71. * @return MorphMany
  72. */
  73. public function files(): MorphMany
  74. {
  75. return $this->morphMany(File::class, 'relate');
  76. }
  77.  
  78. /**
  79. * Возвращает связь с голосованием
  80. *
  81. * @return morphOne
  82. */
  83. public function polling(): morphOne
  84. {
  85. return $this->morphOne(Polling::class, 'relate')->where('user_id', getUser('id'));
  86. }
  87.  
  88. /**
  89. * Удаление фото и загруженных файлов
  90. *
  91. * @return bool|null
  92. * @throws Exception
  93. */
  94. public function delete(): ?bool
  95. {
  96. $this->files->each(static function (File $file) {
  97. $file->delete();
  98. });
  99.  
  100. return parent::delete();
  101. }
  102. }