$anime * @property Collection $artists * @property ImageFacet $facet * @property int $image_id * @property string $link * @property string $mimetype * @property string $path * @property Collection $playlists * @property int $size * @property Collection $studios * * @method static ImageFactory factory(...$parameters) */ #[Appends([Image::ATTRIBUTE_LINK])] #[Table(Image::TABLE, Image::ATTRIBUTE_ID)] class Image extends BaseModel implements Auditable, SoftDeletable { use HasAudits; use HasFactory; use SoftDeletes; use Submitable; final public const string TABLE = 'images'; final public const string ATTRIBUTE_FACET = 'facet'; final public const string ATTRIBUTE_ID = 'image_id'; final public const string ATTRIBUTE_PATH = 'path'; final public const string ATTRIBUTE_LINK = 'link'; final public const string RELATION_ANIME = 'anime'; final public const string RELATION_ARTISTS = 'artists'; final public const string RELATION_PLAYLISTS = 'playlists'; final public const string RELATION_STUDIOS = 'studios'; /** * The event map for the model. * * Allows for object-based events for native Eloquent events. * * @var array */ protected $dispatchesEvents = [ 'created' => ImageCreated::class, 'deleted' => ImageDeleted::class, 'forceDeleting' => ImageForceDeleting::class, 'restored' => ImageRestored::class, 'updated' => ImageUpdated::class, ]; /** * The attributes that are mass assignable. * * @var list */ protected $fillable = [ Image::ATTRIBUTE_FACET, Image::ATTRIBUTE_PATH, ]; /** * Get the attributes that should be cast. * * @return array */ protected function casts(): array { return [ Image::ATTRIBUTE_FACET => ImageFacet::class, Image::ATTRIBUTE_PATH => 'string', ]; } public function getName(): string { return $this->path; } public function getSubtitle(): string { return $this->path; } protected function link(): Attribute { return Attribute::make(function () { if ($this->hasAttribute(Image::ATTRIBUTE_PATH) && $this->exists) { /** @var \Illuminate\Filesystem\FilesystemAdapter $fs */ $fs = Storage::disk(Config::get('image.disk')); return $fs->url($this->getAttribute(Image::ATTRIBUTE_PATH)); } return null; }); } /** * @return MorphToMany */ public function anime(): MorphToMany { return $this->morphedByMany(Anime::class, Imageable::RELATION_IMAGEABLE, Imageable::TABLE, Imageable::ATTRIBUTE_IMAGE, Imageable::ATTRIBUTE_IMAGEABLE_ID) ->using(Imageable::class) ->as('animeimage') ->withPivot([Imageable::ATTRIBUTE_ID, Imageable::ATTRIBUTE_DEPTH]) ->withTimestamps(); } /** * @return MorphToMany */ public function artists(): MorphToMany { return $this->morphedByMany(Artist::class, Imageable::RELATION_IMAGEABLE, Imageable::TABLE, Imageable::ATTRIBUTE_IMAGE, Imageable::ATTRIBUTE_IMAGEABLE_ID) ->using(Imageable::class) ->as('artistimage') ->withPivot([Imageable::ATTRIBUTE_ID, Imageable::ATTRIBUTE_DEPTH]) ->withTimestamps(); } /** * @return MorphToMany */ public function studios(): MorphToMany { return $this->morphedByMany(Studio::class, Imageable::RELATION_IMAGEABLE, Imageable::TABLE, Imageable::ATTRIBUTE_IMAGE, Imageable::ATTRIBUTE_IMAGEABLE_ID) ->using(Imageable::class) ->as('studioimage') ->withPivot([Imageable::ATTRIBUTE_ID, Imageable::ATTRIBUTE_DEPTH]) ->withTimestamps(); } /** * @return MorphToMany */ public function playlists(): MorphToMany { return $this->morphedByMany(Playlist::class, Imageable::RELATION_IMAGEABLE, Imageable::TABLE, Imageable::ATTRIBUTE_IMAGE, Imageable::ATTRIBUTE_IMAGEABLE_ID) ->using(Imageable::class) ->as('playlistimage') ->withPivot([Imageable::ATTRIBUTE_ID, Imageable::ATTRIBUTE_DEPTH]) ->withTimestamps(); } }