diff --git a/app/Http/Controllers/MediaController.php b/app/Http/Controllers/MediaController.php index 1ecd3f0..39d2889 100644 --- a/app/Http/Controllers/MediaController.php +++ b/app/Http/Controllers/MediaController.php @@ -115,13 +115,31 @@ public function index(Request $request) public function show($id) { - $media = Media::with(['image', 'category', 'myNote' , 'tags']) - ->where('id', $id) - ->where('visibility', 'public') - ->orWhere('user_id', auth()->id()) - ->firstOrFail(); + $media = Media::with(['image', 'category', 'myNote', 'tags']) + ->where('id', $id) + ->where(function($query) { + $query->where('visibility', 'public') + ->orWhere('user_id', auth()->id()); + }) + ->firstOrFail(); - return response()->json($media); + return response()->json([ + 'id' => $media->id, + 'title' => $media->title, + 'caption' => $media->caption, + 'type' => $media->type, + 'file_path' => $media->file_path, + 'external_url' => $media->external_url, + 'duration' => $media->duration, + 'visibility' => $media->visibility, + 'created_at' => $media->created_at, + 'updated_at' => $media->updated_at, + 'image' => $media->image, + 'category' => $media->category, + 'tags' => $media->tags, + 'myNote' => $media->myNote, + 'is_saved' => $media->is_saved, + ]); } // UPDATE media diff --git a/app/Models/Media.php b/app/Models/Media.php index 512ceb6..2c7c4a7 100644 --- a/app/Models/Media.php +++ b/app/Models/Media.php @@ -54,4 +54,15 @@ public function getUrlAttribute() return $this->external_url; } + + + public function getIsSavedAttribute() + { + if (!auth()->check()) return false; + + return $this->savedBy() + ->where('user_id', auth()->id()) + ->exists(); + } + }