У меня есть система CRUD специально для обновлений, я пытался обновить данные, и все данные обновляются правильно, кроме изображений.
это мой код контроллера:
Код: Выделить всё
public function update_book(Request $request, $id)
{
// dd($request->all());
$book = Book::find($id);
$imageName = $request->file('image')->store('public/book');
$book->update([
'name' => $request->name,
'date' => $request->date,
'author' => $request->author,
'stock' => $request->stock,
'description' => $request->description,
'image' => $imageName
]);
$book->categories()->sync($request->categories);
return redirect()->route('admin.books_panel')->with('success', 'Data Successfully Updated!');
}
Код: Выделить всё
const [image, setImage] = useState(null)
const handleImageChange = (e: React.ChangeEvent) => {
if (e.target.files && e.target.files[0]) {
setImage(e.target.files[0])
}
}
const updateBook = async (e: FormEvent) => {
e.preventDefault()
let cat: string[] = []
selectedCategories.forEach(categoryId => {
cat.push(categoryId)
})
Inertia.put(`/admin/books-panel/${book.id}`, {
name: name,
date: date,
author: author,
stock: stock,
description: description,
image: image as Blob,
categories: cat.slice(1)
})
}
Код: Выделить всё
array:7 [▼ // app\Http\Controllers\AdminController.php:114
"name" => "ddd"
"date" => "2024-03-05"
"author" => "ccc"
"stock" => 1111
"description" => "ccc"
"image" => null
"categories" => array:2 [▶]
]
Источник: https://stackoverflow.com/questions/781 ... rtia-react