diff --git a/app/Http/Controllers/MediaController.php b/app/Http/Controllers/MediaController.php index f796e4a..1ecd3f0 100644 --- a/app/Http/Controllers/MediaController.php +++ b/app/Http/Controllers/MediaController.php @@ -165,20 +165,39 @@ public function update(Request $request, $id) $data['file_path'] = $request->file('file')->store('media', 'public'); } - // --- merge final category --- - $data['category_id'] = $categoryId; + // --- Prepare update data --- + $updateData = [ + 'title' => $data['title'] ?? $media->title, + 'caption' => $data['caption'] ?? $media->caption, + 'type' => $data['type'] ?? $media->type, + 'category_id' => $categoryId, + 'duration' => $data['duration'] ?? $media->duration, + 'external_url' => $data['external_url'] ?? $media->external_url, + 'visibility' => $data['visibility'] ?? $media->visibility, + 'file_path' => $data['file_path'] ?? $media->file_path, + ]; - // --- update media --- - $media->update($data); - if (isset($data['tags'])) { - $tagIds = []; - - foreach ($data['tags'] as $tagName) { - $tag = Tag::firstOrCreate(['name' => $tagName]); - $tagIds[] = $tag->id; + // --- Handle image_id specifically --- + // If image_id is provided in request, use it (even if null to remove association) + // If not provided, keep the existing value + if (array_key_exists('image_id', $data)) { + $updateData['image_id'] = $data['image_id']; + } else { + $updateData['image_id'] = $media->image_id; } - $media->tags()->sync($tagIds); + // --- update media --- + $media->update($updateData); + + if (isset($data['tags'])) { + $tagIds = []; + + foreach ($data['tags'] as $tagName) { + $tag = Tag::firstOrCreate(['name' => $tagName]); + $tagIds[] = $tag->id; + } + + $media->tags()->sync($tagIds); } return response()->json([ diff --git a/app/Models/Image.php b/app/Models/Image.php index 8aa3d28..9beb62c 100644 --- a/app/Models/Image.php +++ b/app/Models/Image.php @@ -11,4 +11,12 @@ class Image extends Model public function user() { return $this->belongsTo(User::class); } + + public function getUrlAttribute() + { + return asset('storage/' . $this->path); + } + + // Make sure the URL is included when serializing + protected $appends = ['url']; } diff --git a/routes/api.php b/routes/api.php index 0496fcf..4badb6f 100644 --- a/routes/api.php +++ b/routes/api.php @@ -137,7 +137,7 @@ ///media Route::post('/media', [MediaController::class, 'store']); Route::get('/media', [MediaController::class, 'index']); - Route::put('/media/{id}', [MediaController::class, 'update']); + Route::post('/media/{id}', [MediaController::class, 'update']); Route::delete('/media/{id}', [MediaController::class, 'destroy']); Route::get('/media/{id}', [MediaController::class, 'show']); Route::post('/media/{id}/save', [MediaController::class, 'saveMedia']);