代码之家  ›  专栏  ›  技术社区  ›  GemmaM

如何使用laravel控制器更新数据库?MethodNotAllowedHttpException无消息错误消息

  •  0
  • GemmaM  · 技术社区  · 6 年前

    我正在尝试使用我的 编辑刀身php页面如下所示。当字段按预期填写在表单中时,“编辑”部分工作正常,但是当我尝试保存时,会显示一条错误消息

    Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 没有消息

    显示。我已经尝试了很多方法来解决这个问题,但我不知道我会错在哪里。希望这是一个简单的解决方法?

    编辑刀身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');
        }
    

    网状物php

    <?php
    Route::get('edit/{id}', 'PostsController@edit');
    
    Route::put('/edit', 'PostsController@update');
    

    邮递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'
        ]; 
    
    }
    

    我的网站是一个食品日志应用程序,这个功能是为了让他们可以编辑他们的日志。

    非常感谢您的帮助!

    2 回复  |  直到 6 年前
        1
  •  1
  •   Salar Bahador    6 年前

    根据迈克尔·捷克霍夫斯基(MichaelCzechowski),我编辑了我的答案,以便更好地回答这个问题,主要问题在于你的路线内部:

    Route::put('/edit/{id}', 'PostsController@update');
    

    您也必须在管线参数中添加id。update()函数需要两个参数,第一个是公式中的表单参数,第二个是已编辑日志项的$id。

    第二个问题是,表单方法字段是“patch”,而路由方法是“put”。

    “patch”和“put”之间的区别是:

    放置: 获取数据并更新行,然后从要更新的数据在数据库中创建新行。

    修补程序: 只更新该行,不生成新行。

    因此,如果只想更新旧行,请将route方法更改为patch。

    或者,如果确实要放置数据,只需更改表单中的“放置方法”字段。

    简单地说: {{method_field('PUT')}}

    回想起 ,表单和路由的方法必须相同。如果表单的方法已放置,则必须放置路由方法;反之亦然。

        2
  •  0
  •   Michael W. Czechowski    6 年前

    主要问题是路线内部:

    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 可能未在模板内设置。也许可以根据您的帖子指定ID。只是为了确保ID来自显示的帖子。

    进一步建议

    最佳做法是使用symfony内置 FormBuilder 。这样可以更容易地针对以下特殊请求 PUT ,则, PATCH ,则, OPTIONS ,则, DELETE