Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
更新函数在Laravel 5.4中不断给出MethodNotAllowedHttpException_Laravel_View_Controller_Laravel 5.4_Blade - Fatal编程技术网

更新函数在Laravel 5.4中不断给出MethodNotAllowedHttpException

更新函数在Laravel 5.4中不断给出MethodNotAllowedHttpException,laravel,view,controller,laravel-5.4,blade,Laravel,View,Controller,Laravel 5.4,Blade,我试图通过一个表单更新这些值,但是我一次又一次地得到错误。 这是刀片代码 <div class="container" > <div class="row"> <div class="col-md-12" style="margin-top: 50px; padding-left: 25em"> <div class="col-md-3"> <img src=

我试图通过一个表单更新这些值,但是我一次又一次地得到错误。

这是刀片代码

    <div class="container" >
      <div class="row">
   <div class="col-md-12" style="margin-top: 50px; padding-left: 25em">
            <div class="col-md-3">
                <img src="{{$user->photo ? asset($user->photo->file) : 
asset('image/default.png')}}" style="width: 150px; height: 150px; float: 
left; border-radius: 50%; margin-right: 25px">
            </div>
            <div class="col-md-9 ">
                {!! Form::model($user, ['method'=>'PUT' ,'action'=>
['ProfileController@update',$user->id],'files'=>true])!!}
                <div class=form-group style="margin: 50px">
                    <h2>{{$user->name}}'s profile</h2>
                    {!! Form::label('name','Name :') !!}
           {!! Form::text('name', null , ['class'=>'form-control'])!!}
           {!! Form::label('email', 'Email :') !!}
           {!! Form::text('email', null, ['class'=>'form-control']) !!}
           {!! Form::label('photo_id', 'Profile Picture :') !!}
           {!! Form::file('photo_id', null, ['class'=>'form-control']) !!}
           {!! Form::label('password', 'Password:') !!}
         {!! Form::password('password', null, ['class'=>'form-control']) !!}

                </div>
            </div>
     <div class="row" style="padding-top: 20px; padding-left: 50%;">
        {!! Form::submit('Update Profile', ['class'=>'btn btn-info']) !!}
                {!! Form::close() !!}
            </div>
        </div>
    </div>
</div>
我想这可能是因为没有正确分配路由,所以我将它们设置为其他资源


当我试图通过ProfileController@edit我得到了错误。我做了ProfileController@editProfileroute和它开始向我提供视图,但更新仍然不起作用

不更新的问题是,您的表单正在通过PUT提交,并且您声明这是routes中的post调用。因此,下面的任一解决方案都可以解决问题:

解决方案1

update.blade.php

{!! Form::model($user, 
     ['method' => 'PUT',
      'action' => [
         'ProfileController@update',$user->id
      ],
      'files' => true
    ])
!!}
{!! Form::model($user, 
     ['method' => 'POST',
      'action' => [
         'ProfileController@update',$user->id
      ],
      'files' => true
    ])
!!}
web.php

Route::put('/profile/{id}', 'ProfileController@updateProfile')->name('updateProfile');
Route::post('/profile/{id}', 'ProfileController@updateProfile')->name('updateProfile');
解决方案2

update.blade.php

{!! Form::model($user, 
     ['method' => 'PUT',
      'action' => [
         'ProfileController@update',$user->id
      ],
      'files' => true
    ])
!!}
{!! Form::model($user, 
     ['method' => 'POST',
      'action' => [
         'ProfileController@update',$user->id
      ],
      'files' => true
    ])
!!}
web.php

Route::put('/profile/{id}', 'ProfileController@updateProfile')->name('updateProfile');
Route::post('/profile/{id}', 'ProfileController@updateProfile')->name('updateProfile');

问题在于您的资源路由定义。您已经包含了一个尾随斜杠,这会弄乱定义的路线

定义资源路由时,定义中最后一个斜杠后面的项目是资源,而它前面的所有内容都只是路由前缀。因此,使用/profile/的定义,您的资源为空,路由前缀为/profile。这就是为什么没有正确定义路由,并且不能按预期工作的原因

删除尾部斜杠,配置文件资源路由将正常工作:

Route::resource('profile', 'ProfileController');

您还可以取消已定义的两条额外路由。

是否添加了csrf令牌?还可以尝试注释掉Route::get和Route::post路由。您的代码有点混乱,您的路由建议您有两种更新方法,一种称为@update,一种称为@updateProfile,第一种使用put方法,第二种使用post方法,如果您删除之前重复的方法但不起作用@UX Labs也包括了这一点,但它没有解决@connormcwood,那么您应该能够自己解决它