fix: image media

This commit is contained in:
2025-11-27 10:57:11 +03:30
parent 626a95dc57
commit 6f60a2627d
3 changed files with 39 additions and 12 deletions
+30 -11
View File
@@ -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([
+8
View File
@@ -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'];
}