【问题标题】:Update function keeps on giving MethodNotAllowedHttpException in Laravel 5.4更新函数在 Laravel 5.4 中不断给出 MethodNotAllowedHttpException
【发布时间】:2018-07-19 07:15:55
【问题描述】:

尝试通过表单更新值,但我一遍又一遍地收到错误消息。

这是刀片代码

    <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();
  }

我认为这可能是因为没有正确分配路线所以我把它们做成了资源以外的

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

当我试图通过 ProfileController@edit 路由获取视图时,我收到了 错误。我创建了 ProfileController@editProfile 路由,它开始为我提供视图,但更新仍然无法正常工作

【问题讨论】:

  • 你添加了csrf令牌吗?还可以尝试将您的 Route::get 和 Route::post 路由注释掉。
  • 你的代码有点乱,你的路由提示你有两个更新方法,一个叫@update(),一个叫@updateProfile,第一个使用put方法,第二个使用post方法,去掉重复方法应该可以自己解决
  • 以前试过但没用@UX Labs
  • 也包括在内,但它没有解决它@connormcwood

标签: laravel view controller laravel-5.4 blade


【解决方案1】:

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

定义资源路由时,定义中最后一个斜杠后面的项目是资源,它之前的所有内容都只是一个路由前缀。所以,对于/profile/ 的定义,你的资源是空的,你的路由前缀是“/profile”。这就是您的路线未正确定义且未按预期工作的原因。

删除尾部斜杠,您的配置文件资源路由将起作用:

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

你也可以去掉你定义的两条额外的路线。

【讨论】:

    【解决方案2】:

    不更新的问题是您的表单是通过PUT 提交的,并且您在路由中声明这是一个post call。所以下面的任何一种解决方案都可以解决这个问题:

    解决方案 1

    update.blade.php

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

    web.php

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

    解决方案 2

    update.blade.php

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

    web.php

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

    【讨论】:

      猜你喜欢
      • 2018-04-13
      • 2017-12-21
      • 2018-02-28
      • 1970-01-01
      • 1970-01-01
      • 2018-05-28
      • 1970-01-01
      • 2017-10-25
      • 1970-01-01
      相关资源
      最近更新 更多