belongsTo(Image::class); } // Image shown on the detail (show-by-id) screen; image() is the list thumbnail. public function detailImage() { return $this->belongsTo(Image::class, 'detail_image_id'); } public function categories() { return $this->belongsToMany(Category::class, 'category_media'); } public function subCategories() { return $this->belongsToMany(SubCategory::class, 'media_sub_category'); } public function tags() { return $this->belongsToMany(Tag::class, 'media_tag'); } public function plays() { return $this->hasMany(MediaPlay::class); } public function notes() { return $this->morphMany(Note::class, 'noteable'); } public function myNote() { return $this->morphOne(Note::class, 'noteable')->where('user_id', auth()->id()); } public function savedBy() { return $this->belongsToMany(User::class, 'saved_media') ->withTimestamps(); } public function getUrlAttribute() { if ($this->file_path) return asset('storage/' . $this->file_path); return $this->external_url; } public function getIsSavedAttribute() { if (!auth()->check()) return false; return $this->savedBy() ->where('user_id', auth()->id()) ->exists(); } }