feat: add is_saved function
This commit is contained in:
@@ -115,13 +115,31 @@ public function index(Request $request)
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$media = Media::with(['image', 'category', 'myNote' , 'tags'])
|
||||
$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
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user