Reputation: 29
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.
this is the Error.
Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException No message 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');
This is the 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();
}
}
This is the Model 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
@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
Upvotes: 0
Views: 15557
Reputation: 6011
The main reason you're getting this error is because you set your form to submit with a PATCH
method and you've set your route to look for a PUT
method.
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
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
Then your resource route would look something like:
Route::resource('admin_users', 'AdminController');
Upvotes: 0
Reputation: 76
I can't comment for clarification but is it not because you need to allow the put
method through the routes? :)
Route::put($uri, $callback);
from the docs here:
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');
Put
isn't a method I use a massive amount though so I'm talking purely from my experience with Laravel rather than working with put
requests themseleves so apologies if I've gone down the wrong path :)
Upvotes: 2