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条)

报告相同问题?

悬赏问题

  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,