feat: add is_saved function

This commit is contained in:
2025-11-29 11:17:45 +03:30
parent 6f60a2627d
commit c9d442223c
2 changed files with 35 additions and 6 deletions
+21 -3
View File
@@ -117,11 +117,29 @@ public function show($id)
{
$media = Media::with(['image', 'category', 'myNote', 'tags'])
->where('id', $id)
->where('visibility', 'public')
->orWhere('user_id', auth()->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
+11
View File
@@ -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();
}
}