Mugluck
Mugluck

Reputation: 499

Very Confusing MethodNotAllowedHttpException on a put request laravel

So far all attempts to modify the routing methods have failed.

Been following some documentation on laravel restful controllers and have one set up to do basic editing and adding of items to a database. It was going well till I hit the snag on... well I'm not sure what precisely is triggering the problem, but basically, everything works till I hit submit on the form and then it's Game Over.

Normally I'd be able to diagnose this by checking to see if I'm using the right call, or made a spelling mistake or something. But this is a new request for me, so I can't quite debug where the problem is coming from.

This is the error those who know what to look for. In full here.

MethodNotAllowedHttpException in RouteCollection.php line 218:

My routes are pasted here. A printout of the routes is here:

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests\ContactFormRequest;
use App\UserEdit;
use DB;
use App\Http\Requests;

class EditUserController extends Controller
{

    public function index()
    {
        $array = UserEdit::all()->toArray();
        return view('UserEntry', compact('array'));
    }


    public function create()
    {
        $id = UserEdit::find(715)->toArray();
        return view('NewUser', compact('id'));
    }


    public function store(UserFormRequest $request)
    {
        //$user = new UserEdit([
        //    'name'=>$request->get('First_Name'),
        //    'email'=>$request->get('email'),
        //    'username'=>$request->get('name')
        //]);
        //
        //$user->save();
        //return \Redirect::route('users')->with('message', 'Nice Work.');
    }


    public function show($id)
    {
        try {
         $array = UserEdit::findorFail($id)->toArray();
         return view('UserEdit')->with('array', $array);

         } catch(\Exception $e) {
             return \Redirect::route('users.index')
                ->withMessage('This user does not exist');
         }

    }


    public function edit($id)
    {
        $user = UserEdit::findorFail($id);
        return view('EditUser')->with('user',$user);
    }


    public function update($id, UserFormRequest $request)
    {
       $user = UserEdit::findorFail($id);

       $user->update([
           'name' => $request->get('name'),
           'email' => $request->get('email')
       ]);

       return \Redirect::route('users.edit', [$user->id])->with('message', 'Details Updated!');
    }


    public function destroy($id)
    {
        //
    }
}

The Blade is here.

Upvotes: 0

Views: 761

Answers (3)

Mugluck
Mugluck

Reputation: 499

It seems like after sorting out the routes, the issue fell to a bad capitalisation. $user->id should have been $user->ID.

Upvotes: 0

Vision Coderz
Vision Coderz

Reputation: 8078

i think you are using model in form.try this

 {{ Form::open(['method' => 'put',  'route'=>['users.update', $user->id], 'class'=>'form']) }}

As per your route list and route put doesnt taking id so you get method not found exception

PUT users/{user}    App\Http\Controllers\EditUserController@update

instead of using resouce just type each route for each method

Route::put('users/{user}', 'EditUserController @update');

Upvotes: 1

Beginner
Beginner

Reputation: 4153

if you have a hard time finding the solution the easiest solution is using

Route::any('users/{user}', 'UserEntryController@update');

this allow you to access this action with any method type
OR

Route::match(array('get', 'put'), 'users/{user}', 'UserEntryController@update');

so you need 2 method which are

get -> view
put -> submit update

you can just indicate which method type you want to be accessible with in this action

Upvotes: 1

Related Questions