代码之家  ›  专栏  ›  技术社区  ›  Constantine

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

  •  0
  • Constantine  · 技术社区  · 7 年前

    我是 尝试通过表单更新值 但我一次又一次地犯错误。 error on update

    这是刀片代码

        <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>
    

    这是控制器部分

     public function updateProfile(UserRequest $request, $id)
       {
        $user = User::findOrFail($id);
            if (trim($request->password)==''){
                $input = $request->except('password');
            }
            else{
                $input = $request->all();
                $input['password'] = bcrypt($request->password);
            }
            if ($request->file('photo_id'== '')){
                $input = $request->except('photo_id');
            }
            elseif ($file = $request->file('photo_id')){
                $name = time() . $file->getClientOriginalName();
                $file->move('image', $name);
                $photo = Photo::create(['file'=>$name]);
                $input['photo_id'] = $photo->id;
            }
            $user->update($input);
        //  Session::flash('edited_profile','The profile has been updated');
       // $input['password'] = bcrypt($request->password);
            return redirect('/');
      // return $request->all();
      }
    

    我想这可能是因为没有正确分配路线,所以我将其设置为其他资源 routes list

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

    当我试图通过ProfileController@edit我的路线 error on edit view 错误。我做了ProfileController@editProfile路线,它开始给我视图,但更新仍然不起作用

    2 回复  |  直到 7 年前
        1
  •  1
  •   patricus    7 年前

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

    定义资源路由时,定义中最后一个斜杠后的项目是资源,它之前的所有内容都只是路由前缀。所以,根据 /profile/ ,您的资源为空,路由前缀为“/配置文件”。这就是为什么您的管线定义不正确,无法按预期工作的原因。

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

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

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

        2
  •  0
  •   Nikola Gavric    7 年前

    未更新的问题是,您的表单是通过 PUT 你还说这是你路线上的一个电话留言。因此,下面的任一解决方案都可以解决问题:

    解决方案1

    使现代化刀身php

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

    网状物php

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

    解决方案2

    使现代化刀身php

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

    网状物php

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