martyn martyn - 1 month ago 24
PHP Question

Laravel 5 form submit creates an error MethodNotAllowedHttpException

I'm using Laravel 5. When editing a form a get this error

MethodNotAllowedHttpException


There are a few posts but I think it may be code specific. I'd rather not use the resource route methods if possible. Route:

Route::get('profile/edit', array('as' => 'admin.profile.edit', 'uses' => 'ProfileController@edit'));
Route::post('profile/edit', array('as' => 'admin.profile.update', 'uses' => 'ProfileController@update'));


The form is :

{!!Form::model($profile,array('method'=>'Patch','route' =>array('admin.profile.update'))) !!}


Controller:

public function update()
{
$user_id=$this->user->id;
$input=array_except(Input::all(),'_method');
Profile::where('user_id','=',$user_id)->update($input);
return Redirect::route('admin.profile.index');

}


Here is the error info:

n RouteCollection.php line 207
at RouteCollection->methodNotAllowed(array('GET', 'HEAD', 'POST')) in RouteCollection.php line 194
at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD', 'POST')) in RouteCollection.php line 142
at RouteCollection->match(object(Request)) in Router.php line 729
at Router->findRoute(object(Request)) in Router.php line 652
at Router->dispatchToRoute(object(Request)) in Router.php line 628
at Router->dispatch(object(Request)) in Kernel.php line 214
at Kernel->Illuminate\Foundation\Http\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 141
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in VerifyCsrfToken.php line 43
at VerifyCsrfToken->handle(object(Request), object(Closure)) in VerifyCsrfToken.php line 17
at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in ShareErrorsFromSession.php line 55
at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in StartSession.php line 61
at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 36
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in EncryptCookies.php line 40
at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in CheckForMaintenanceMode.php line 42
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 125
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 101
at Pipeline->then(object(Closure)) in Kernel.php line 115
at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 84
at Kernel->handle(object(Request)) in index.php line 53


Thanks any help appreciated

Answer

Can you change this:

Route::post('profile/edit', array(
  'as' => 'admin.profile.update', 
  'uses' => 'ProfileController@update'
));

To this:

Route::patch('profile/edit', array(
  'as' => 'admin.profile.update', 
  'uses' => 'ProfileController@update'
));

I think your form action may need a matching route verb

http://laravel.com/docs/5.1/routing#basic-routing

For the second issue:

Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update user_profile` set _method = PATCH, _token =

Add this to the top of your controller

use Illuminate\Http\Request;

Then update your method as so:

public function update(Request $request)
{
    $user_id = $this->user->id;

    $input = $request->except('_method', '_token');

    Profile::where('user_id','=', $user_id)->update($input);

    return Redirect::route('admin.profile.index');

}
Comments