fix: image media
This commit is contained in:
@@ -165,20 +165,39 @@ public function update(Request $request, $id)
|
|||||||
$data['file_path'] = $request->file('file')->store('media', 'public');
|
$data['file_path'] = $request->file('file')->store('media', 'public');
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- merge final category ---
|
// --- Prepare update data ---
|
||||||
$data['category_id'] = $categoryId;
|
$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 ---
|
// --- Handle image_id specifically ---
|
||||||
$media->update($data);
|
// If image_id is provided in request, use it (even if null to remove association)
|
||||||
if (isset($data['tags'])) {
|
// If not provided, keep the existing value
|
||||||
$tagIds = [];
|
if (array_key_exists('image_id', $data)) {
|
||||||
|
$updateData['image_id'] = $data['image_id'];
|
||||||
foreach ($data['tags'] as $tagName) {
|
} else {
|
||||||
$tag = Tag::firstOrCreate(['name' => $tagName]);
|
$updateData['image_id'] = $media->image_id;
|
||||||
$tagIds[] = $tag->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([
|
return response()->json([
|
||||||
|
|||||||
@@ -11,4 +11,12 @@ class Image extends Model
|
|||||||
public function user() {
|
public function user() {
|
||||||
return $this->belongsTo(User::class);
|
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
|
///media
|
||||||
Route::post('/media', [MediaController::class, 'store']);
|
Route::post('/media', [MediaController::class, 'store']);
|
||||||
Route::get('/media', [MediaController::class, 'index']);
|
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::delete('/media/{id}', [MediaController::class, 'destroy']);
|
||||||
Route::get('/media/{id}', [MediaController::class, 'show']);
|
Route::get('/media/{id}', [MediaController::class, 'show']);
|
||||||
Route::post('/media/{id}/save', [MediaController::class, 'saveMedia']);
|
Route::post('/media/{id}/save', [MediaController::class, 'saveMedia']);
|
||||||
|
|||||||
Reference in New Issue
Block a user