| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
-
- namespace App\Models;
-
- use Illuminate\Database\Eloquent\Casts\Attribute;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Facades\Storage;
- use Spatie\Translatable\HasTranslations;
-
- class Project extends Model
- {
- use HasTranslations, SoftDeletes;
-
- protected $guarded = ['id'];
-
- protected $translatable = ['name', 'sub_name', 'summaries', 'img_alt', 'address', 'floor_plan',
- 'building_structure', 'design_unit', 'contact_unit', 'contact_phone', 'inversment_phone', 'district',
- ];
-
- protected $casts = ['img_url' => 'array'];
-
- protected $appends = ['first_list_img_url', 'img_list', 'thumbnail_url'];
-
- public function region()
- {
- return $this->belongsTo(Region::class);
- }
-
- public function tags()
- {
- return $this->morphToMany(Tag::class, 'taggable');
- }
-
- public function badges()
- {
- return $this->morphToMany(Badge::class, 'badgeable')
- ->withPivot('award_date', 'award_type') // ✅ 關鍵
- ->withTimestamps();
- }
-
- /**
- * 永續目標
- */
- public function targetBadges()
- {
- return $this->morphToMany(Badge::class, 'badgeable')
- ->withPivot('award_type')
- ->wherePivot('award_type', 1)
- ->withTimestamps();
- }
-
- /**
- * 取得標章
- */
- public function awardBadges()
- {
- return $this->morphToMany(Badge::class, 'badgeable')
- ->withPivot('award_type', 'award_date')
- ->wherePivot('award_type', 2)
- ->withTimestamps();
- }
-
- public function histories()
- {
- return $this->hasMany(ProjectHistory::class);
- }
-
- public function spaceInfos()
- {
- return $this->hasMany(ProjectSpaceInfo::class);
- }
-
- public function firstListImgUrl(): Attribute
- {
- return Attribute::make(
- get: fn ($value) => ! empty($this->img_url) ? Storage::url($this->img_url[0]) : null,
- );
- }
-
- public function imgList(): Attribute
- {
- $imgList = [];
- if (! is_null($this->img_url) && count($this->img_url) > 0) {
- foreach ($this->img_url as $img) {
- $imgList[] = Storage::url($img);
- }
- }
-
- return Attribute::make(
- get: fn ($value) => $imgList,
- );
- }
-
- public function thumbnailUrl(): Attribute
- {
- return Attribute::make(
- get: fn ($value) => ! empty($this->thumbnail) ? Storage::url($this->thumbnail) : null,
- );
- }
-
- public function getBadges($locale): array
- {
- $badges = [];
- if ($this->badges->count() > 0) {
- foreach ($this->badges as $badge) {
- $badges[] = [
- 'imgUrl' => $badge->imgUrlLink,
- 'name' => $badge->getTranslation('title', $locale),
- 'awardDate' => '取得時間 : '.date('Y.m', strtotime($badge->pivot->award_date)),
- ];
- }
- }
-
- return $badges;
- }
-
- public function getBadgesByType($locale, $badgeType): array
- {
- $badges = [];
- switch ($badgeType) {
- case '1':
- $badgesData = $this->targetBadges;
- foreach ($badgesData as $badge) {
- $badges[] = [
- 'imgUrl' => $badge->imgUrlLink,
- 'name' => $badge->getTranslation('title', $locale),
- 'awardDate' => null,
- ];
- }
- break;
- case '2':
- $badgesData = $this->awardBadges;
- foreach ($badgesData as $badge) {
- $badges[] = [
- 'imgUrl' => $badge->imgUrlLink,
- 'name' => $badge->getTranslation('title', $locale),
- 'awardDate' => '取得時間 : '.date('Y.m', strtotime($badge->pivot->award_date)),
- ];
- }
- break;
- }
-
- return $badges;
- }
-
- public function getTags($locale): array
- {
- $tags = [];
- if ($this->tags->count() > 0) {
- foreach ($this->tags as $tag) {
- $tags[] = [
- 'id' => $tag->id,
- 'name' => $tag->getTranslation('name', $locale),
- ];
- }
- }
-
- return $tags;
- }
- }
|