fix: image media
This commit is contained in:
@@ -165,11 +165,30 @@ 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,
|
||||
];
|
||||
|
||||
// --- 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;
|
||||
}
|
||||
|
||||
// --- update media ---
|
||||
$media->update($data);
|
||||
$media->update($updateData);
|
||||
|
||||
if (isset($data['tags'])) {
|
||||
$tagIds = [];
|
||||
|
||||
|
||||
@@ -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'];
|
||||
}
|
||||
|
||||
+1
-1
@@ -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']);
|
||||
|
||||
Reference in New Issue
Block a user