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

Размер файла: 2.03Kb
  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\MorphMany;
  10. use Illuminate\Database\Eloquent\Relations\MorphOne;
  11.  
  12. /**
  13. * Class Photo
  14. *
  15. * @property int id
  16. * @property int user_id
  17. * @property string title
  18. * @property string text
  19. * @property int created_at
  20. * @property int rating
  21. * @property int closed
  22. * @property int count_comments
  23. * @property Collection files
  24. */
  25. class Photo 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/photos';
  49.  
  50. /**
  51. * Morph name
  52. *
  53. * @var string
  54. */
  55. public static $morphName = 'photos';
  56.  
  57. /**
  58. * Возвращает комментарии фотографий
  59. *
  60. * @return MorphMany
  61. */
  62. public function comments(): MorphMany
  63. {
  64. return $this->morphMany(Comment::class, 'relate')->with('relate');
  65. }
  66.  
  67. /**
  68. * Возвращает загруженные файлы
  69. *
  70. * @return MorphMany
  71. */
  72. public function files(): MorphMany
  73. {
  74. return $this->morphMany(File::class, 'relate');
  75. }
  76.  
  77. /**
  78. * Возвращает связь с голосованием
  79. *
  80. * @return morphOne
  81. */
  82. public function polling(): morphOne
  83. {
  84. return $this->morphOne(Polling::class, 'relate')->where('user_id', getUser('id'));
  85. }
  86.  
  87. /**
  88. * Удаление фото и загруженных файлов
  89. *
  90. * @return bool|null
  91. */
  92. public function delete(): ?bool
  93. {
  94. $this->files->each(static function (File $file) {
  95. $file->delete();
  96. });
  97.  
  98. return parent::delete();
  99. }
  100. }