dongshen7561 2018-04-26 21:19
浏览 96
已采纳

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

I'm trying to update my database using a form on my edit.blade.php page as shown below. The edit part works correctly as the fields are filled in in the form as expected, however when i try to save, an error message of

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message

is displayed. I have tried so many ways on how to fix it and I'm not sure where I'm going wrong. Hopefully it's something simple to fix?

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'
    ]; 

}

My website is a food log application and this function is so that they can edit their log.

Any help is greatly appreciated!

  • 写回答

2条回答 默认 最新

  • dosf40815 2018-04-26 21:29
    关注

    Based on Michael Czechowski I edited my answer to make this answer better, The main problem is inside your routes:

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

    You have to add the id inside your route parameters either. Your update() function needs two parameters, first the form parameters from the formular and second the $id of the edited log entry.

    The second problem is , the form method field is 'patch' and your route method is 'put'.

    The difference between 'patch' and 'put' is:

    put: gets the data and update the row and makes a new row in the database from the data that you want to update.

    patch: just updates the row and it does not make a new row.

    so if you want to just update the old row change the route method to patch.

    or if you really want to put the data, just change the put method field in your form.

    simply by : {{method_field('PUT')}}

    Remember, the form's and the route's methods must be same. If the form's method is put, the route method must be put; and vice-versa.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站