Search code examples
phpformslaravel-5putlaravel-blade

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.


Solution

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