我整天都收到此错误,似乎无法正确解决。我的目标基本上是更新admin_users表中的用户,以更改其角色。
这是错误。
Symfony \组件\ HttpKernel \ Exception \ MethodNotAllowedHttpException没有消息Laravel 5.7
Web.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');
这是Controller 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();
}
}
这是模型Admin_users.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Admin_users extends Model
{
protected $guarded=[];
//public function admin_users(){
// }
}
这是刀片服务器模板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
答案 0 :(得分:2)
我无法发表评论,不是因为您需要允许put
方法通过路线吗? :)
Route::put($uri, $callback);
来自此处的文档:
https://laravel.com/docs/5.7/routing
所以我相信答案是:
Route::put('/admin_users', 'AdminUController@admin_users')->name('admin_users.update');
Put
并不是我大量使用的一种方法,所以我纯粹是根据我在Laravel上的经验来谈论,而不是与put
一起使用,如果我不赞成的话,他们会道歉错误的路径:)
答案 1 :(得分:0)
出现此错误的主要原因是,您将表单设置为使用PATCH
方法提交,并且设置了查找PUT
方法的路线。
您还可以将路线设置为:
Route::match(['put', 'patch'], '/admin_users/update/{id}','AdminController@update');
或者,您可以使用php artisan make:controller AdminUController --resource
Laravel资源路由通过单行代码将典型的“ CRUD”路由分配给控制器。例如,您可能希望创建一个控制器来处理admin_users的所有HTTP请求
那么您的资源路线将类似于:
Route::resource('admin_users', 'AdminController');