我正在使用 Laravel,我正在尝试创建一个编辑页面并在提交时调用我的更新方法,问题是我在更新时收到 404。这是我的 Blade 文件,用于编辑:
@extends('adminlte::page')
@section('title', 'AdminLTE')
@section('content_header')
<h1>Professions</h1>
@stop
@section('content')
<form method="PUT" action="/admin/professions-update/{{ $data->pkprofession }}">
<div class="form-group">
<label for="profession_name">Profession Name</label>
<input type="text" name="profession_name" id="profession_name" class="form-control" value="{{$data->profession_name}}" />
</div>
<div class="form-group">
<button type="submit" class="btn btn-success">Update</button>
</div>
</form>
@stop
这是我的路线:
Route::get('/admin/professions-edit/{id}', 'v1\ProfessionsController@edit');
Route::put('/admin/professions-update/{id}', 'v1\ProfessionsController@update');
下面是被调用的方法:
public function edit($id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
public function update(Request $request, $id)
{
$data = PdTprofession::find($id);
return view('professions-edit', compact('data'));
}
为什么会出现 404 错误,我该如何解决?
谢谢,
最佳答案
In laravel docs, HTML forms do not support PUT, PATCH or DELETE actions. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:
<form action="/foo/bar" method="POST">
<input type="hidden" name="_method" value="PUT">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
您可以使用 @method Blade 指令来生成 _method 输入:
<form action="/foo/bar" method="POST">
@method('PUT')
@csrf
</form>
关于php - Laravel - 编辑和更新页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54555602/