Project.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Database\Eloquent\Casts\Attribute;
  4. use Illuminate\Database\Eloquent\Model;
  5. use Illuminate\Database\Eloquent\SoftDeletes;
  6. use Illuminate\Support\Facades\Storage;
  7. use Spatie\Translatable\HasTranslations;
  8. class Project extends Model
  9. {
  10. use HasTranslations, SoftDeletes;
  11. protected $guarded = ['id'];
  12. protected $translatable = ['name', 'sub_name', 'summaries', 'img_alt', 'address', 'floor_plan',
  13. 'building_structure', 'design_unit', 'contact_unit', 'contact_phone', 'inversment_phone', 'district',
  14. ];
  15. protected $casts = ['img_url' => 'array', 'mobile_img_url' => 'array'];
  16. protected $appends = ['first_list_img_url', 'img_list', 'thumbnail_url', 'first_mobile_img_url', 'mobile_img_list'];
  17. public function region()
  18. {
  19. return $this->belongsTo(Region::class);
  20. }
  21. public function tags()
  22. {
  23. return $this->morphToMany(Tag::class, 'taggable');
  24. }
  25. public function badges()
  26. {
  27. return $this->morphToMany(Badge::class, 'badgeable')
  28. ->withPivot('award_date', 'award_type') // ✅ 關鍵
  29. ->withTimestamps();
  30. }
  31. /**
  32. * 永續目標
  33. */
  34. public function targetBadges()
  35. {
  36. return $this->morphToMany(Badge::class, 'badgeable')
  37. ->withPivot('award_type')
  38. ->wherePivot('award_type', 1)
  39. ->withTimestamps();
  40. }
  41. /**
  42. * 取得標章
  43. */
  44. public function awardBadges()
  45. {
  46. return $this->morphToMany(Badge::class, 'badgeable')
  47. ->withPivot('award_type', 'award_date')
  48. ->wherePivot('award_type', 2)
  49. ->withTimestamps();
  50. }
  51. public function histories()
  52. {
  53. return $this->hasMany(ProjectHistory::class);
  54. }
  55. public function spaceInfos()
  56. {
  57. return $this->hasMany(ProjectSpaceInfo::class);
  58. }
  59. public function firstListImgUrl(): Attribute
  60. {
  61. return Attribute::make(
  62. get: fn ($value) => ! empty($this->img_url) ? Storage::url($this->img_url[0]) : null,
  63. );
  64. }
  65. public function imgList(): Attribute
  66. {
  67. $imgList = [];
  68. if (! is_null($this->img_url) && count($this->img_url) > 0) {
  69. foreach ($this->img_url as $img) {
  70. $imgList[] = Storage::url($img);
  71. }
  72. }
  73. return Attribute::make(
  74. get: fn ($value) => $imgList,
  75. );
  76. }
  77. public function thumbnailUrl(): Attribute
  78. {
  79. return Attribute::make(
  80. get: fn ($value) => ! empty($this->thumbnail) ? Storage::url($this->thumbnail) : null,
  81. );
  82. }
  83. public function firstMobileImgUrl(): Attribute
  84. {
  85. return Attribute::make(
  86. get: fn ($value) => ! empty($this->mobile_img_url) ? Storage::url($this->mobile_img_url[0]) : null,
  87. );
  88. }
  89. public function mobileImgList(): Attribute
  90. {
  91. $imgList = [];
  92. if (! is_null($this->mobile_img_url) && count($this->mobile_img_url) > 0) {
  93. foreach ($this->mobile_img_url as $img) {
  94. $imgList[] = Storage::url($img);
  95. }
  96. }
  97. return Attribute::make(
  98. get: fn ($value) => $imgList,
  99. );
  100. }
  101. public function getBadges($locale): array
  102. {
  103. $badges = [];
  104. if ($this->badges->count() > 0) {
  105. foreach ($this->badges as $badge) {
  106. $badges[] = [
  107. 'imgUrl' => $badge->imgUrlLink,
  108. 'name' => $badge->getTranslation('title', $locale),
  109. 'awardDate' => '取得時間 : '.date('Y.m', strtotime($badge->pivot->award_date)),
  110. ];
  111. }
  112. }
  113. return $badges;
  114. }
  115. public function getBadgesByType($locale, $badgeType): array
  116. {
  117. $badges = [];
  118. switch ($badgeType) {
  119. case '1':
  120. $badgesData = $this->targetBadges;
  121. foreach ($badgesData as $badge) {
  122. $badges[] = [
  123. 'imgUrl' => $badge->imgUrlLink,
  124. 'name' => $badge->getTranslation('title', $locale),
  125. 'awardDate' => null,
  126. ];
  127. }
  128. break;
  129. case '2':
  130. $badgesData = $this->awardBadges;
  131. foreach ($badgesData as $badge) {
  132. $badges[] = [
  133. 'imgUrl' => $badge->imgUrlLink,
  134. 'name' => $badge->getTranslation('title', $locale),
  135. 'awardDate' => '取得時間 : '.date('Y.m', strtotime($badge->pivot->award_date)),
  136. ];
  137. }
  138. break;
  139. }
  140. return $badges;
  141. }
  142. public function getTags($locale): array
  143. {
  144. $tags = [];
  145. if ($this->tags->count() > 0) {
  146. foreach ($this->tags as $tag) {
  147. $tags[] = [
  148. 'id' => $tag->id,
  149. 'name' => $tag->getTranslation('name', $locale),
  150. ];
  151. }
  152. }
  153. return $tags;
  154. }
  155. }