Laravel 5表单提交会创建一个错误MethodNotAllowedHttpException

时间:2015-06-11 08:03:22

标签: php forms controller routes laravel-5

我正在使用Laravel 5.编辑表单时出现此错误

MethodNotAllowedHttpException

有一些帖子,但我认为它可能是特定于代码的。如果可能的话,我宁愿不使用资源路由方法。路线:

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

表格是:

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

控制器:

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');

}

以下是错误信息:

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

感谢任何帮助

2 个答案:

答案 0 :(得分:2)

你能改变一下吗?

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

对此:

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

我认为您的表单操作可能需要匹配的路线动词

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

关于第二个问题:

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

将其添加到控制器顶部

use Illuminate\Http\Request;

然后更新您的方法:

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');

}

答案 1 :(得分:0)

在我的案例中,可以在模型文件中添加protected $guarded = ['_token'];