Laravel 9对于此路线不支持PUT方法。支持的方法:获取,头
我只是在尝试更新数据时遇到错误。我正在使用帖子方法而不是推动。我尝试添加@method('put'),但这也没有用。
创建新类别的工作正常工作,但是当我尝试编辑某些内容时,它会引发错误。我也试图使用路线:: put,但这无效。
我的类别controller.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$data = Category::all();
return view('admin.category.index', [
'data' => $data
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.category.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = new Category;
$data->parent_id = 1;
$data->title = $request->title;
$data->keywords = $request->keywords;
$data->description = $request->description;
$data->status = $request->status;
$data->save();
return redirect('admin/category');
}
/**
* Display the specified resource.
*
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function show(Category $category)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function edit(Category $category, $id)
{
//
$data = Category::find($id);
return view('admin.category.edit', [
'data' => $data
]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Category $category, $id)
{
//
$data = Category::find($id);
$data->parent_id = 1;
$data->title = $request->title;
$data->keywords = $request->keywords;
$data->description = $request->description;
$data->status = $request->status;
$data->save();
return redirect('admin/category');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function destroy(Category $category)
{
//
}
}
我的edit.blade.php
@extends('layouts.adminbase')
@section('title', 'Edit Category')
@section('content')
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">Edit Category: {{$data -> title}}</h1>
<!-- Collapsable Card-->
<div class="card shadow mb-4">
<!-- Card Header - Accordion -->
<a href="#collapseCardExample" class="d-block card-header py-3" data-toggle="collapse"
role="button" aria-expanded="true" aria-controls="collapseCardExample">
<h6 class="m-0 font-weight-bold text-primary">Category Elements</h6>
</a>
<!-- Card Content - Collapse -->
<div class="collapse show" id="collapseCardExample">
<div class="card-body">
<form action="/admin/category/update/{{$data->id}}" method="POST">
@csrf
<div class="card-body">
<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<input type="text" class="form-control" name="title" value="{{$data->title}}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Keywords</label>
<input type="text" class="form-control" name="keywords" value="{{$data->keywords}}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Description</label>
<input type="text" class="form-control" name="description" value="{{$data->description}}">
</div>
<div class="form-group">
<label for="exampleInputFile">Image</label>
<div class="input-group">
<div class="custom-file">
<input type="file" class="custom-file-input" name="image">
<label class="custom-file-label" for="exampleInputFile">Choose image file</label>
</div>
<div class="input-group-append">
<span class="input-group-text">Upload</span>
</div>
</div>
</div>
<div class="form-group">
<label>Status</label>
<select class="custom-select" name="status">
<option selected>{{$data->status}}</option>
<option>Enable</option>
<option>Disable</option>
</select>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Update Data</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
@endsection
我的路线
// Admin category routes
Route::get('/admin/category', [AdminCategoryController::class,'index'] )->name('admin_category');
Route::get('/admin/category/create', [AdminCategoryController::class,'create'] )->name('admin_category_create');
Route::post('/admin/category/store', [AdminCategoryController::class,'store'] )->name('admin_category_store');
Route::get('/admin/category/edit/{id}', [AdminCategoryController::class,'edit'] )->name('admin_category_edit');
Route::post('/admin/category/update/{id}', [AdminCategoryController::class,'edit'] )->name('admin_category_update');
I'm only getting an error while trying to update the data. I'm using post method not the push. I've tried to add @method('PUT') but that didn't work, too.
Creating new category just works fine as it should do but when I try to edit something it just throws the error. Also I tried to use route::put but that didn't work.
My CategoryController.php
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Category;
use Illuminate\Http\Request;
class CategoryController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
$data = Category::all();
return view('admin.category.index', [
'data' => $data
]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.category.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$data = new Category;
$data->parent_id = 1;
$data->title = $request->title;
$data->keywords = $request->keywords;
$data->description = $request->description;
$data->status = $request->status;
$data->save();
return redirect('admin/category');
}
/**
* Display the specified resource.
*
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function show(Category $category)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function edit(Category $category, $id)
{
//
$data = Category::find($id);
return view('admin.category.edit', [
'data' => $data
]);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Category $category, $id)
{
//
$data = Category::find($id);
$data->parent_id = 1;
$data->title = $request->title;
$data->keywords = $request->keywords;
$data->description = $request->description;
$data->status = $request->status;
$data->save();
return redirect('admin/category');
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Category $category
* @return \Illuminate\Http\Response
*/
public function destroy(Category $category)
{
//
}
}
MY edit.blade.php
@extends('layouts.adminbase')
@section('title', 'Edit Category')
@section('content')
<!-- Begin Page Content -->
<div class="container-fluid">
<!-- Page Heading -->
<h1 class="h3 mb-4 text-gray-800">Edit Category: {{$data -> title}}</h1>
<!-- Collapsable Card-->
<div class="card shadow mb-4">
<!-- Card Header - Accordion -->
<a href="#collapseCardExample" class="d-block card-header py-3" data-toggle="collapse"
role="button" aria-expanded="true" aria-controls="collapseCardExample">
<h6 class="m-0 font-weight-bold text-primary">Category Elements</h6>
</a>
<!-- Card Content - Collapse -->
<div class="collapse show" id="collapseCardExample">
<div class="card-body">
<form action="/admin/category/update/{{$data->id}}" method="POST">
@csrf
<div class="card-body">
<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<input type="text" class="form-control" name="title" value="{{$data->title}}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Keywords</label>
<input type="text" class="form-control" name="keywords" value="{{$data->keywords}}">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Description</label>
<input type="text" class="form-control" name="description" value="{{$data->description}}">
</div>
<div class="form-group">
<label for="exampleInputFile">Image</label>
<div class="input-group">
<div class="custom-file">
<input type="file" class="custom-file-input" name="image">
<label class="custom-file-label" for="exampleInputFile">Choose image file</label>
</div>
<div class="input-group-append">
<span class="input-group-text">Upload</span>
</div>
</div>
</div>
<div class="form-group">
<label>Status</label>
<select class="custom-select" name="status">
<option selected>{{$data->status}}</option>
<option>Enable</option>
<option>Disable</option>
</select>
</div>
</div>
<!-- /.card-body -->
<div class="card-footer">
<button type="submit" class="btn btn-primary">Update Data</button>
</div>
</form>
</div>
</div>
</div>
</div>
<!-- /.container-fluid -->
</div>
<!-- End of Main Content -->
@endsection
My routes
// Admin category routes
Route::get('/admin/category', [AdminCategoryController::class,'index'] )->name('admin_category');
Route::get('/admin/category/create', [AdminCategoryController::class,'create'] )->name('admin_category_create');
Route::post('/admin/category/store', [AdminCategoryController::class,'store'] )->name('admin_category_store');
Route::get('/admin/category/edit/{id}', [AdminCategoryController::class,'edit'] )->name('admin_category_edit');
Route::post('/admin/category/update/{id}', [AdminCategoryController::class,'edit'] )->name('admin_category_update');
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将对您的代码进行这些修改:
edit.blade:
路线:
检查是否有效。
还有一件事:如果您正确创建 /a>,您的更新函数看起来像这样:
那是因为您已经在那里拥有模型:
public函数更新(请求$请求,**类别$类别**,$ id)
I'd make these modifications to your code:
edit.blade:
Route:
Check if it works.
One more thing: If you correctly created model binding, your update function could look like this:
That is because you already have the model right there:
public function update(Request $request, **Category $category**, $id)