dongshen7561
dongshen7561
2018-04-26 21:19

如何使用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 dosf40815 3年前

    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.

    点赞 评论 复制链接分享
  • dongwei5794 dongwei5794 3年前

    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 one is inside your HTML template:

    <input type="hidden" name="_method" value="PUT" />
    

    To hit the right route you have to add the corresponding method to your route Route::put('/edit/{id}', 'PostsController@update');.

    A possible last problem

    <form method="post" action="{{ action('PostsController@update', $post->id) }}">
    

    I am not sure how your template works, but $id is possible not set inside your template. Maybe try to specify the ID depending on your post. Just to make it sure the ID comes from the shown post.

    Further suggestions

    Best practice is to use the symfony built-in FormBuilder. This would make it easier to target those special requests like PUT, PATCH, OPTIONS, DELETE etc.

    点赞 评论 复制链接分享