Laravel 5.7 错误消息“Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 无消息”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53529052/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:10:41  来源:igfitidea点击:

Laravel 5.7 Error message " Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message"

phplaravel

提问by Delroy Brown

I have been getting this error all day and cant seem to get it right. my goal is basically to update a user in the admin_users table, in order to change their rolls.

我一整天都收到这个错误,似乎无法正确解决。我的目标基本上是更新 admin_users 表中的用户,以更改他们的角色。

this is the Error.

这是错误。

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message Laravel 5.7

Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException 无消息 Laravel 5.7

Web.php

网页.php

Route::get('/dashboard', 'DashboardController@dashboard');
Route::post('/admin_users', 'AdminUController@admin_users')->name('admin_users.update');
Route::get('/admin_users', 'AdminUController@admin_users')->name('admin_users');

This is the Controller AdminUController.php

这是控制器 AdminUController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Post;
use Mail;
use Session;
use Gate;
use App\Admin_users;

class AdminUController extends Controller
{

    public function admin_users(){

        if(!Gate::allows('isUser')){
            if(!Gate::allows('isAdmin')){
                abort(404,"Sorry, There is nothing to see here!");
            }
       }

        // data from users table to be passed to the view
        $admin_users = Admin_users::all();

         $title = 'Earn, Invest, Save, and Learn with the Invo App';
         return view('admin_users', ['admin_users' => $admin_users])->with('title',$title);
     }

     public function update(Request $request, Admin_users $admin_users){
        $admin_users->email = $request->email;
        $admin_users->user_type = $request->user_type;
        $admin_users->save();
        session()->flash('User account has been updated!');
        return redirect()->back();
     }
}

This is the Model Admin_users.php

这是模型 Admin_users.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Admin_users extends Model
{
    protected $guarded=[];
    //public function admin_users(){

   // }
}

This is the blade template admin_users.blade.php

这是刀片模板 admin_users.blade.php

@extends('layouts.app_dashboard')
@section('content')
<div class="col-md-12">
    <div class="margin-top card strpied-tabled-with-hover">
            <div class="card-header ">
                <h4 class="card-title">Invo Admin Users</h4>
                <p class="card-category">Click to edit each user</p>
                <a href="{{ route('register') }}" class="btn btn-primary margin-top">
                        Add New User
                </a>
            </div>
            <div class="card-body table-full-width table-responsive">
                <table class="table table-hover table-striped">
                    <thead>
                        <tr><th>ID</th>
                        <th>Name</th>
                        <th>Email</th>
                        <th>Password </th>
                        <th>User Access</th>
                    </tr></thead>
                    <tbody>
                        @foreach ($admin_users as $user)
                        @if(session()->has('message'))
                            <div class="alert alert-success">
                                {{session()->get('message')}}
                            </div>
                        @endif
                        <form method="POST" action="{{route('admin_users.update', $user->id)}}">
                         @csrf
                         @method('put')
                        <tr>
                            <td>{{$user->id}}</td>
                        <td>{{$user->name}}</td>
                            <td>
                                <input type="email" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="{{$user->email}}">
                            </td>
                            <input type="file" class="custom-file-input" value="" placeholder="{{$user->email}}">
                            <td>*********</td>
                            <td>
                                <select class="selectpicker">
                                    @if(Auth::id() == $user->id)
                                        <option selected value="{{$user->user_type}}">{{$user->user_type}}</option>
                                        <option value="user">user</option>
                                    @else
                                        <option selected value="{{$user->user_type}}">{{$user->user_type}}</option>
                                        <div role="separator" class="dropdown-divider"></div>
                                        <a class="dropdown-item" href="#">Change User Type to</a>
                                        @if($user->user_type == 'admin')
                                            <option value="user">user</option>
                                        @else
                                            <option value="admin">admin</option>
                                        @endif
                                    @endif


                                </select> 
                            </td>
                            <td>
                                <button type="submit" class="btn btn-primary">
                                    Update
                                </button>
                            </td>
                        </tr>
                        </form>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    @endsection

回答by shane-chris-barker

I can't comment for clarification but is it not because you need to allow the putmethod through the routes? :)

我无法评论澄清,但这不是因为您需要允许该put方法通过路由吗?:)

Route::put($uri, $callback);

Route::put($uri, $callback);

from the docs here:

从这里的文档:

https://laravel.com/docs/5.7/routing

https://laravel.com/docs/5.7/routing

So I believe the answer to be:

所以我相信答案是:

Route::put('/admin_users', 'AdminUController@admin_users')->name('admin_users.update');

Route::put('/admin_users', 'AdminUController@admin_users')->name('admin_users.update');

Putisn't a method I use a massive amount though so I'm talking purely from my experience with Laravel rather than working with putrequests themseleves so apologies if I've gone down the wrong path :)

Put不是我大量使用的方法,所以我纯粹是根据我在 Laravel 的经验而不是处理put请求本身,如果我走错了路,我深表歉意:)

回答by Ismoil Shifoev

The main reason you're getting this error is because you set your form to submit with a PATCHmethod and you've set your route to look for a PUTmethod.

您收到此错误的主要原因是您将表单设置为使用PATCH方法提交,并且您已将路由设置为查找PUT方法。

you could also set your route to:

您还可以将路线设置为:

Route::match(['put', 'patch'], '/admin_users/update/{id}','AdminController@update');

Alternatively, you can use php artisan make:controller AdminUController --resource

或者,您可以使用 php artisan make:controller AdminUController --resource

Laravel resource routing assigns the typical "CRUD" routes to a controller with a single line of code. For example, you may wish to create a controller that handles all HTTP requests for admin_users

Laravel 资源路由使用一行代码将典型的“CRUD”路由分配给控制器。例如,您可能希望创建一个控制器来处理 admin_users 的所有 HTTP 请求

Then your resource route would look something like:

然后你的资源路线看起来像:

Route::resource('admin_users', 'AdminController');