Album.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Casts\Attribute;
  4. use Illuminate\Database\Eloquent\Factories\HasFactory;
  5. use Illuminate\Database\Eloquent\Model;
  6. use Illuminate\Database\Eloquent\SoftDeletes;
  7. use Illuminate\Support\Facades\Storage;
  8. use Spatie\Translatable\HasTranslations;
  9. class Album extends Model
  10. {
  11. use HasFactory, SoftDeletes, HasTranslations;
  12. protected $casts = [
  13. 'on_top' => 'boolean',
  14. "list_audit_state" => "string"
  15. ];
  16. protected $guarded = ['id'];
  17. protected $appends = ["album_img_pc_url", "album_img_pc_url", "album_img_banner_url", "album_img_mobile_url", "album_meta_img", "album_upload_type", "album_video_link"];
  18. public $translatable = ['title', 'description', 'meta_title', 'meta_description', 'meta_keyword'];
  19. public function albumCategory(){
  20. return $this->belongsTo(AlbumCategory::class);
  21. }
  22. protected function albumImgPcUrl(): Attribute
  23. {
  24. return Attribute::make(
  25. get: fn ($value) => is_null($this->news_img_pc) ? null :Storage::disk('s3')->url($this->news_img_pc),
  26. );
  27. }
  28. protected function albumImgMobileUrl(): Attribute
  29. {
  30. return Attribute::make(
  31. get: fn ($value) => is_null($this->news_img_mobile) ? null :Storage::disk('s3')->url($this->news_img_mobile),
  32. );
  33. }
  34. protected function albumMetaImg(): Attribute
  35. {
  36. return Attribute::make(
  37. get: fn ($value) => is_null($this->meta_img) ? null :Storage::disk('s3')->url($this->meta_img),
  38. );
  39. }
  40. protected function albumImgBannerUrl(): Attribute
  41. {
  42. return Attribute::make(
  43. get: fn ($value) => is_null($this->news_banner) ? null :Storage::disk('s3')->url($this->news_banner),
  44. );
  45. }
  46. protected function albumUploadType(): Attribute
  47. {
  48. return Attribute::make(
  49. get: fn ($value) => $this->attributes["upload_type"] == 1 ? "url" : "upload",
  50. );
  51. }
  52. protected function listAuditState(): Attribute
  53. {
  54. return Attribute::make(
  55. get: fn ($value) => $this->attributes["visible"] == 1 ? "已發佈" : "暫存",
  56. );
  57. }
  58. protected function albumVideoLink(): Attribute
  59. {
  60. return Attribute::make(
  61. get: fn ($value) => ($this->attributes["upload_type"] == 2) ? Storage::disk('s3')->url($this->attributes["link"]) : $this->attributes["link"],
  62. );
  63. }
  64. public function homepageToTop()
  65. {
  66. return $this->morphOne(HomepageToTop::class, 'resource');
  67. }
  68. protected static function booted()
  69. {
  70. static::created(function ($album) {
  71. if ($album->homepage_top) {
  72. $album->createOrUpdatePageSet();
  73. }
  74. });
  75. static::updated(function ($album) {
  76. if ($album->isDirty('homepage_top')) {
  77. if ($album->homepage_top) {
  78. $album->createOrUpdatePageSet();
  79. } else {
  80. $album->homepageToTop()->delete();
  81. }
  82. }
  83. });
  84. static::deleted(function ($album) {
  85. $album->homepageToTop()->delete();
  86. });
  87. }
  88. protected function createOrUpdatePageSet()
  89. {
  90. $this->homepageToTop()->updateOrCreate(
  91. [],
  92. ['resource' => 'video']
  93. );
  94. }
  95. }