Project.php 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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"
  14. ];
  15. protected $casts = ["img_url" => "array"];
  16. protected $appends = ["first_list_img_url", "img_list", "meta_img_url"];
  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') // ✅ 關鍵
  29. ->withTimestamps();
  30. }
  31. /**
  32. * 永續目標
  33. */
  34. public function targetBadges()
  35. {
  36. return $this->morphToMany(Badge::class, 'badgeable')
  37. ->using(Badgeable::class)
  38. ->wherePivot('award_type', 1)
  39. ->withPivot('award_type')
  40. ->withTimestamps();
  41. }
  42. /**
  43. * 取得標章
  44. */
  45. public function awardBadges()
  46. {
  47. return $this->morphToMany(Badge::class, 'badgeable')
  48. ->using(Badgeable::class)
  49. ->wherePivot('award_type', 2)
  50. ->withPivot('award_type', 'award_date')
  51. ->withTimestamps();
  52. }
  53. public function histories()
  54. {
  55. return $this->hasMany(ProjectHistory::class);
  56. }
  57. public function spaceInfos()
  58. {
  59. return $this->hasMany(ProjectSpaceInfo::class);
  60. }
  61. protected function metaImgUrl(): Attribute
  62. {
  63. return Attribute::make(
  64. get: fn ($value) => is_null($this->meta_img) ? null :Storage::url($this->meta_img),
  65. );
  66. }
  67. public function firstListImgUrl(): Attribute
  68. {
  69. return Attribute::make(
  70. get: fn ($value) => is_null($this->img_url) ? null :Storage::url($this->img_url[0]),
  71. );
  72. }
  73. public function imgList(): Attribute
  74. {
  75. $imgList = [];
  76. if(!is_null($this->img_url) && count($this->img_url) > 0){
  77. foreach($this->img_url as $img){
  78. $imgList[] = Storage::url($img);
  79. }
  80. }
  81. return Attribute::make(
  82. get: fn ($value) => $imgList,
  83. );
  84. }
  85. public function getBadges($locale) : array
  86. {
  87. $badges = [];
  88. if($this->badges->count() > 0){
  89. foreach($this->badges as $badge){
  90. $badges[] = [
  91. "imgUrl" => $badge->imgUrlLink,
  92. "name" => $badge->getTranslation("name", $locale),
  93. "rewardYear" => "取得年份 : " . $badge->reward_year
  94. ];
  95. }
  96. }
  97. return $badges;
  98. }
  99. public function getBadgesByType($locale, $badgeType) : array
  100. {
  101. $badges = [];
  102. switch($badgeType){
  103. case "1":
  104. $badgesData = $this->targetBadges();
  105. foreach($badgesData as $badge){
  106. $badges[] = [
  107. "imgUrl" => $badge->imgUrlLink,
  108. "name" => null,
  109. "rewardYear" => null
  110. ];
  111. }
  112. break;
  113. case "2":
  114. $badgesData = $this->awardBadges();
  115. foreach($badgesData as $badge){
  116. $badges[] = [
  117. "imgUrl" => $badge->imgUrlLink,
  118. "name" => $badge->getTranslation("name", $locale),
  119. "rewardYear" => "取得年份 : " . $badge->award_date
  120. ];
  121. }
  122. break;
  123. }
  124. return $badges;
  125. }
  126. public function getTags($locale) : array
  127. {
  128. $tags = [];
  129. if($this->tags->count() > 0){
  130. foreach($this->tags as $tag){
  131. $tags[] = [
  132. "id" => $tag->id,
  133. "name" => $tag->getTranslation("name", $locale),
  134. ];
  135. }
  136. }
  137. return $tags;
  138. }
  139. }