如何使用 laravel 控制器更新数据库? MethodNotAllowedHttpException No message 错误信息
how to update database using laravel controller? MethodNotAllowedHttpException No message error message
我正在尝试使用我的表格更新我的数据库
edit.blade.php页面如下图。编辑部分工作正常,因为字段按预期填写在表单中,但是当我尝试保存时,错误消息
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException
No message
显示。我已经尝试了很多方法来修复它,但我不确定我哪里出错了。希望这很容易解决?
edit.blade.php
@extends('layouts.app')
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<form method="post" action="{{ action('PostsController@update', $id) }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH" />
<h1>Edit Item</h1>
<div class="form-group">
<label for="item">Item:</label>
<input type="text" id="item" name="item" value="{{$post->item}}" class="form-control" required>
</div>
<div class="form-group">
<label for="weight">Weight (g):</label>
<input type="number" id="weight" value="{{$post->weight}}" name="weight" class="form-control">
</div>
<div class="form-group">
<label for="noofservings">No of Servings:</label>
<input type="number" id="noofservings" value="{{$post->noofservings}}" name="noofservings" class="form-control">
</div>
<div class="form-group">
<label for="calories">Calories (kcal):</label>
<input type="number" id="calories" name="calories" value="{{$post->calories}}" class="form-control">
</div>
<div class="form-group">
<label for="fat">Fat (g):</label>
<input type="number" id="fat" name="fat" value="{{$post->fat}}" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
</div>
</div>
@endsection
PostsController.php
<?php
public function update(Request $request, $id)
{
$this->validate('$request', [
'item' => 'required'
]);
$post = Post::find($id);
$post->item = $request->input('item');
$post->weight = $request->input('weight');
$post->noofservings = $request->input('noofservings');
$post->calories = $request->input('calories');
$post->fat = $request->input('fat');
$post->save();
return redirect('/foodlog');
}
web.php
<?php
Route::get('edit/{id}', 'PostsController@edit');
Route::put('/edit', 'PostsController@update');
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'id',
'user_id',
'item',
'weight',
'noofservings',
'calories',
'fat',
'created_at'
];
}
我的网站是一个食物日志应用程序,这个功能是为了让他们可以编辑他们的日志。
非常感谢任何帮助!
基于 Michael Czechowski,我编辑了我的答案以使这个答案更好,主要问题在你的路线中:
Route::put('/edit/{id}', 'PostsController@update');
您也必须在路由参数中添加 id。您的 update() 函数需要两个参数,第一个是公式中的表单参数,第二个是已编辑日志条目的 $id。
第二个问题,表单方法字段是'patch',你的路由方法是'put'。
'patch'和'put'的区别是:
put: 获取数据并更新行,并根据要更新的数据在数据库中创建一个新行。
patch: 只是更新行,它不会生成新行。
因此,如果您只想更新旧行,请将路由方法更改为补丁。
或者如果你真的想放入数据,只需更改表单中的放置方法字段即可。
只需:{{method_field('PUT')}}
记住,表单和路由的方法必须相同。如果表单的方法是put,路由方法必须是put;反之亦然。
主要问题出在你的路由里面:
Route::put('/edit/{id}', 'PostsController@update');
您也必须在路由参数中添加 id
。您的 update()
函数需要两个参数,第一个是公式中的表单参数,第二个是已编辑日志条目的 $id
。
第二个在您的 HTML 模板中:
<input type="hidden" name="_method" value="PUT" />
要找到正确的路线,您必须将相应的 method 添加到您的路线 Route::put('/edit/{id}', 'PostsController@update');
。
最后一个可能的问题
<form method="post" action="{{ action('PostsController@update', $post->id) }}">
我不确定你的模板是如何工作的,但是 $id
可能没有在你的模板中设置。也许尝试根据您的 post 指定 ID。只是为了确保 ID 来自显示的 post.
进一步的建议
最佳做法是使用内置的 symfony FormBuilder。这样可以更轻松地针对 PUT
、PATCH
、OPTIONS
、DELETE
等特殊请求
我正在尝试使用我的表格更新我的数据库 edit.blade.php页面如下图。编辑部分工作正常,因为字段按预期填写在表单中,但是当我尝试保存时,错误消息
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message
显示。我已经尝试了很多方法来修复它,但我不确定我哪里出错了。希望这很容易解决?
edit.blade.php
@extends('layouts.app')
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
@section('content')
<div class="container">
<div class="row justify-content-center">
<div class="col-md-8">
<form method="post" action="{{ action('PostsController@update', $id) }}">
{{ csrf_field() }}
<input type="hidden" name="_method" value="PATCH" />
<h1>Edit Item</h1>
<div class="form-group">
<label for="item">Item:</label>
<input type="text" id="item" name="item" value="{{$post->item}}" class="form-control" required>
</div>
<div class="form-group">
<label for="weight">Weight (g):</label>
<input type="number" id="weight" value="{{$post->weight}}" name="weight" class="form-control">
</div>
<div class="form-group">
<label for="noofservings">No of Servings:</label>
<input type="number" id="noofservings" value="{{$post->noofservings}}" name="noofservings" class="form-control">
</div>
<div class="form-group">
<label for="calories">Calories (kcal):</label>
<input type="number" id="calories" name="calories" value="{{$post->calories}}" class="form-control">
</div>
<div class="form-group">
<label for="fat">Fat (g):</label>
<input type="number" id="fat" name="fat" value="{{$post->fat}}" class="form-control">
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>
</div>
</div>
</div>
@endsection
PostsController.php
<?php
public function update(Request $request, $id)
{
$this->validate('$request', [
'item' => 'required'
]);
$post = Post::find($id);
$post->item = $request->input('item');
$post->weight = $request->input('weight');
$post->noofservings = $request->input('noofservings');
$post->calories = $request->input('calories');
$post->fat = $request->input('fat');
$post->save();
return redirect('/foodlog');
}
web.php
<?php
Route::get('edit/{id}', 'PostsController@edit');
Route::put('/edit', 'PostsController@update');
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
protected $fillable = [
'id',
'user_id',
'item',
'weight',
'noofservings',
'calories',
'fat',
'created_at'
];
}
我的网站是一个食物日志应用程序,这个功能是为了让他们可以编辑他们的日志。
非常感谢任何帮助!
基于 Michael Czechowski,我编辑了我的答案以使这个答案更好,主要问题在你的路线中:
Route::put('/edit/{id}', 'PostsController@update');
您也必须在路由参数中添加 id。您的 update() 函数需要两个参数,第一个是公式中的表单参数,第二个是已编辑日志条目的 $id。
第二个问题,表单方法字段是'patch',你的路由方法是'put'。
'patch'和'put'的区别是:
put: 获取数据并更新行,并根据要更新的数据在数据库中创建一个新行。
patch: 只是更新行,它不会生成新行。
因此,如果您只想更新旧行,请将路由方法更改为补丁。
或者如果你真的想放入数据,只需更改表单中的放置方法字段即可。
只需:{{method_field('PUT')}}
记住,表单和路由的方法必须相同。如果表单的方法是put,路由方法必须是put;反之亦然。
主要问题出在你的路由里面:
Route::put('/edit/{id}', 'PostsController@update');
您也必须在路由参数中添加 id
。您的 update()
函数需要两个参数,第一个是公式中的表单参数,第二个是已编辑日志条目的 $id
。
第二个在您的 HTML 模板中:
<input type="hidden" name="_method" value="PUT" />
要找到正确的路线,您必须将相应的 method 添加到您的路线 Route::put('/edit/{id}', 'PostsController@update');
。
最后一个可能的问题
<form method="post" action="{{ action('PostsController@update', $post->id) }}">
我不确定你的模板是如何工作的,但是 $id
可能没有在你的模板中设置。也许尝试根据您的 post 指定 ID。只是为了确保 ID 来自显示的 post.
进一步的建议
最佳做法是使用内置的 symfony FormBuilder。这样可以更轻松地针对 PUT
、PATCH
、OPTIONS
、DELETE
等特殊请求