| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?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"
- ];
- protected $casts = ["img_url" => "array"];
-
- protected $appends = ["first_list_img_url", "img_list", "meta_img_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');
- }
-
- public function histories()
- {
- return $this->hasMany(ProjectHistory::class);
- }
-
- public function spaceInfos()
- {
- return $this->hasMany(ProjectSpaceInfo::class);
- }
-
- protected function metaImgUrl(): Attribute
- {
- return Attribute::make(
- get: fn ($value) => is_null($this->meta_img) ? null :Storage::url($this->meta_img),
- );
- }
-
- public function firstListImgUrl(): Attribute
- {
- return Attribute::make(
- get: fn ($value) => is_null($this->img_url) ? null :Storage::url($this->img_url[0]),
- );
- }
-
- 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 getBadges($locale) : array
- {
- $badges = [];
- if($this->badges->count() > 0){
- foreach($this->badges as $badge){
- $badges[] = [
- "imgUrl" => $badge->imgUrlLink,
- "name" => $badge->getTranslation("name", $locale),
- "rewardYear" => "取得年份 : " . $badge->reward_year
- ];
- }
- }
-
- 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;
- }
- }
|