尝试更新数据,但显示错误“此路由不支持POST方法。受支持的方法:GET,HEAD。”
路线:
Route::prefix('admin')->namespace('Admin')->name('admin.')->group(function () {
Route::resource('category','CategoryController');
});
控制器:
public function edit(Category $category)
{
$categories = Category::findOrFail($category->id);
return view('admin.category.edit', compact('categories'));
}
public function update(Request $request, Category $category)
{
$request->validate([
'category_name' => 'required|unique:categories',
'category_description' => 'required',
'category_slug' => 'required',
'category_image' => 'required|image',
]);
$category = Category::find($category->id);
$category->category_name = $request->category_name;
$category->category_description = $request->category_description;
$category->category_slug = $request->category_slug;
$path = $request->file('category_image');
$image = $path->getClientOriginalName();
$path->move(public_path('images/backend_images/category_images'), $image);
$category->category_image = $image;
$category->save();
return redirect('admin.category.index')->back()->with('flash_message_success', 'Category has been updated successfully');
刀片文件:
<form method="POST" action="{{ route('admin.category.update', $category->id) }}" enctype="multipart/form-data">
@csrf
@method('PATCH')
答案 0 :(得分:0)
<?php
public function update(Request $request, Category $category)
{
$_val = request()->validate([
'category_name' => 'required|unique:categories',
'category_description' => 'required',
'category_slug' => 'required',
'category_image' => 'required|image',
]);
$path = $request->file('category_image');
$image = $path->getClientOriginalName();
$path->move(public_path('images/backend_images/category_images'), $image);
$_val['category_image'] = $image;
$category = Category::find($request->id);
$category->update($_val);
return redirect('admin.category.index')->back()->with('flash_message_success', 'Category has been updated successfully');
}