【问题标题】:Laravel collective custom forms, action Controller@update not definedLaravel 集体自定义表单,动作 Controller@update 未定义
【发布时间】:2018-11-22 07:29:21
【问题描述】:

这是我的路线:

Route::get('admin/edit-news/{id}', 'AdminNewsController@edit');

我的 Controller@update 方法:

 public function update(Request $request, $id)
    {
        $news = News::find($id);

        $news->title = $request->input('title');
        $news->content = $request->input('content');

        $news->save();

        return redirect ('/admin');
    }

以及我对自定义表单的看法:

{{ Form::open(['action' => ['AdminNewsController@update', $news->id], 'method' => 'POST']) }}   
                {{ Form::bsText('title', $news->title) }}
                {{ Form::bsTextArea('content', $news->content) }}
                {{ Form::hidden('_method', 'PUT') }}            
                {{ Form::bsSubmit('Confirm', ['class' => 'btn btn-primary center-block']) }}
            {!! Form::close() !!}

我得到的错误是

“Action App\Http\Controllers\AdminNewsController@update 未定义。(查看:D:\xampp\htdocs\laravelhotel\resources\views\admin\news\edit_news.blade.php)”

我不知道为什么,因为我放的动作是更新功能,并且我在 FormServiceProvider 中注册了所有组件。

【问题讨论】:

  • POST 路由是什么样的?
  • 我只是注意到我根本忘了放它!这么愚蠢的错误。谢谢!
  • 现在是我的发布路线 Route::post('admin/edit-news/submit', 'AdminNewsController@update');给出这个错误 MethodNotAllowedHttpException No message

标签: forms laravel-5.7 laravelcollective


【解决方案1】:

如果使用PUT方式,通过POST表单方式和_method字段({{ Form::hidden('_method', 'PUT') }})模拟,需要使用对应的路由:

Route::put('admin/edit-news/submit', 'AdminNewsController@update'); 
//     ^^^

【讨论】:

  • 是的,我搜索了它并尝试了它,并得到了 2 个参数的错误,1 个丢失(需要 $id 和请求)。
  • 它需要这样的路由:Route::put('admin/edit-news/submit/{id}', 'AdminNewsController@update');获取第二个参数。谢谢你的回答伙伴!
猜你喜欢
  • 2017-10-23
  • 2020-12-29
  • 2015-07-01
  • 2018-11-09
  • 2012-12-21
  • 2021-08-11
  • 2021-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多