【问题标题】:UrlGenerationException: Missing required parameters for [Route: topics.update] [URI: topics/{topic}]UrlGenerationException:缺少 [Route:topics.update] [URI:topics/{topic}] 所需的参数
【发布时间】:2018-06-08 02:05:42
【问题描述】:

我收到此错误:

Missing required parameters for [Route: topics.update] [URI: topics/{topic}]. (View: C:\xampp\htdocs\phpboards\resources\views\topics\edit.blade.php)

这是用户编辑的链接:

<a href="/boards/topics/edit/{{$topic->id}}" class="btn btn-default">Edit</a>

这是编辑控制器:

$topic = Topic::find($id);
return view('topics.edit')->with('topic', $topic);

这是路线:

Route::get('/boards/topics/edit/{id}', 'TopicController@edit');

这是编辑表格:

<div class="container">
    {!! Form::open(['action' => 'TopicController@update', 'method' => 'POST']) !!}
        <div class="form-group">
            {{ Form::label('title', 'Title') }}
            {{ Form::text('title', $topic->topic_title, ['class' => 'form-control', 'placeholder' => 'Title of the Post']) }}
        </div>
        <div class="form-group">
            {{ Form::label('desc', 'Desc') }}
            {{ Form::textarea('desc', $topic->topic_body, ['class' => 'form-control', 'placeholder' => 'Description of the Post']) }}
        </div>
        {{ Form::submit('Submit', ['class' => 'btn btn-default']) }}
    {!! Form::close() !!}
</div>

我在这里做错了什么??

【问题讨论】:

  • @ParantapParashar 但是为了什么?和哪个控制器??
  • 你的代码有很多错误。
  • 我认为你必须把你的更新方法和路线也放在这里。

标签: laravel laravel-5 laravel-routing laravelcollective laravel-form


【解决方案1】:

代替:

{!! Form::open(['action' => 'TopicController@update', 'method' => 'POST']) !!}

使用

{!! Form::open(['url' => route('topics.update', $topic->id), 'method' => 'POST']) !!}

因为对于您的路线,您需要传递要更新的主题的 ID。此外,使用 named routes 而不是 Controller@method 表示法更合理。

【讨论】:

    【解决方案2】:

    让我们承认您的 update() 方法已经在您的 TopicController 上实现。

    首先你需要声明另一个路由:

    Route::put('/boards/topics/edit/{id}', 'TopicController@update');
    //     ^^^
    

    然后通过以下方式更改您的表单打开方式:

    {!! Form::open(['action' => ['TopicController@update', $topic->id], 'method' => 'put']) !!}
    //                                                     ^^^^^^^^^^                ^^^
    

    它应该可以工作。

    【讨论】:

    • 你确定你修改了你的Form::open参数吗?试试这个命令:artisan route:clear &amp;&amp; artisan cache:clear
    • 上述解决方案有效。我没有传递帖子的ID。那是造成错误。
    • 是的,我看到了,这就是我建议您这样做的原因。我真的建议您命名您的路线并使用put 而不是post 来更新资源。 (根据 RESTful v3)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-19
    • 1970-01-01
    • 2017-02-27
    • 2021-09-02
    • 2021-09-19
    • 2019-01-09
    • 2020-08-07
    相关资源
    最近更新 更多