| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
-
- namespace App\Models;
-
- use Illuminate\Database\Eloquent\Casts\Attribute;
- use Illuminate\Database\Eloquent\Factories\HasFactory;
- use Illuminate\Database\Eloquent\Model;
- use Illuminate\Database\Eloquent\SoftDeletes;
- use Illuminate\Support\Facades\Storage;
- use Spatie\Translatable\HasTranslations;
-
- class Album extends Model
- {
- use HasFactory, SoftDeletes, HasTranslations;
-
- protected $casts = [
- 'on_top' => 'boolean',
- "list_audit_state" => "string"
- ];
-
- protected $guarded = ['id'];
- 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"];
-
- public $translatable = ['title', 'description', 'meta_title', 'meta_description', 'meta_keyword'];
-
- public function albumCategory(){
- return $this->belongsTo(AlbumCategory::class);
- }
- protected function albumImgPcUrl(): Attribute
- {
- return Attribute::make(
- get: fn ($value) => is_null($this->news_img_pc) ? null :Storage::disk('s3')->url($this->news_img_pc),
- );
- }
- protected function albumImgMobileUrl(): Attribute
- {
- return Attribute::make(
- get: fn ($value) => is_null($this->news_img_mobile) ? null :Storage::disk('s3')->url($this->news_img_mobile),
- );
- }
- protected function albumMetaImg(): Attribute
- {
- return Attribute::make(
- get: fn ($value) => is_null($this->meta_img) ? null :Storage::disk('s3')->url($this->meta_img),
- );
- }
- protected function albumImgBannerUrl(): Attribute
- {
- return Attribute::make(
- get: fn ($value) => is_null($this->news_banner) ? null :Storage::disk('s3')->url($this->news_banner),
- );
- }
- protected function albumUploadType(): Attribute
- {
- return Attribute::make(
- get: fn ($value) => $this->attributes["upload_type"] == 1 ? "url" : "upload",
- );
- }
-
- protected function listAuditState(): Attribute
- {
- return Attribute::make(
- get: fn ($value) => $this->attributes["visible"] == 1 ? "已發佈" : "暫存",
- );
- }
- protected function albumVideoLink(): Attribute
- {
- return Attribute::make(
- get: fn ($value) => ($this->attributes["upload_type"] == 2) ? Storage::disk('s3')->url($this->attributes["link"]) : $this->attributes["link"],
- );
- }
- public function homepageToTop()
- {
- return $this->morphOne(HomepageToTop::class, 'resource');
- }
- protected static function booted()
- {
- static::created(function ($album) {
- if ($album->homepage_top) {
- $album->createOrUpdatePageSet();
- }
- });
-
- static::updated(function ($album) {
- if ($album->isDirty('homepage_top')) {
- if ($album->homepage_top) {
- $album->createOrUpdatePageSet();
- } else {
- $album->homepageToTop()->delete();
- }
- }
- });
- static::deleted(function ($album) {
- $album->homepageToTop()->delete();
- });
- }
-
- protected function createOrUpdatePageSet()
- {
- $this->homepageToTop()->updateOrCreate(
- [],
- ['resource' => 'video']
- );
- }
- }
|