Album.php 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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::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::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::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::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::url($this->attributes["link"]) : $this->attributes["link"],
  62. );
  63. }
  64. }