Laravel 5.2缺少[Route:showtopic]所需的参数[URI:theme / {id} / topics / {id}]

时间:2017-06-17 07:51:07

标签: php laravel laravel-5 laravel-5.2

我一直收到以下错误:

Missing required parameters for [Route: showtopic] [URI: theme/{id}/topics/{id}].

这就是我的Web.php的样子:

关于web.php中主题的所有内容

Route::get('/theme/{id}/topics/{id}', 'TopicsController@show')->name('showtopic');

Route::get('/theme/{id}/topics/{id}/edit', 'TopicsController@edit')->name('edittopic');
Route::patch('/theme/{id}/topics/{id}/edit', 'TopicsController@update')->name('updatetopic');

Route::get('/theme/{id}/topics/create', 'TopicsController@create')->name('createtopic');
Route::post('/theme/{id}/topics/create', 'TopicsController@save')->name('savetopic');

Route::delete('/theme/{id}/topics/{id}/delete', 'TopicsController@destroy')->name('deletetopic');

关于web.php中主题的所有内容

Route::get('/theme/{id}/topics', 'ThemesController@show')->name('showtheme');

Route::get('/theme/{id}/edit', 'ThemesController@edit')->name('edittheme');
Route::patch('/theme/{id}/edit', 'ThemesController@update')->name('updatetheme');

Route::get('/theme/create', 'ThemesController@create')->name('createtheme');
Route::post('/theme/create', 'ThemesController@save')->name('savetheme');

Route::delete('/theme/{id}/delete', 'ThemesController@destroy')->name('deletetheme');

我在这个问题上提出了关于这个主题的每条路线。所以当我点击我视图中的链接时:

<a href="{{ route('showtopic', ['id' => $topic->id]) }}"

我一直收到我在这个问题开头所显示的错误 我希望我明白这一点,让你明白。如果我错过了这个问题所需的信息,请通知我。提前致谢

Theme.blade.php

  <div class="col s12">
            <div class="card">
                <div class="card-content"><span class="card-title"> - Topics</span>
                    <div class="collection">
                        @foreach($topics as $topic)
                            <a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}" class="collection-item avatar collection-link"><img src="/uploads/avatars/{{ $topic->user->avatar }}" alt="" class="circle">
                                <div class="row">
                                    <div class="col s6">
                                        <div class="row last-row">
                                            <div class="col s12"><span class="title">Theme - {{ $topic->topic_title }}</span>
                                                <p>{!! str_limit($topic->topic_text, $limit = 125, $end = '...') !!}</p>
                                            </div>
                                        </div>
                                        <div class="row last-row">
                                            <div class="col s12 post-timestamp">Posted by: {{ $topic->user->username }} op: {{  $topic->created_at }}</div>
                                        </div>
                                    </div>
                                    <div class="col s2">
                                        <h6 class="title center-align">Replies</h6>
                                        <p class="center replies">{{ $topic->replies->count() }}</p>
                                    </div>
                                    <div class="col s2">
                                        <h6 class="title center-align">Status</h6>
                                        <div class="status-wrapper center-align"><span class="status-badge status-open">open</span></div>
                                    </div>
                                    <div class="col s2">
                                        <h6 class="title center-align">Last reply</h6>
                                        <p class="center-align"></p>
                                        <p class="center-align">Tijd</p>
                                    </div>
                                </div>
                            </a>
                        @endforeach
                    </div>
                </div>

2 个答案:

答案 0 :(得分:1)

您必须为路线参数指定两个不同的参数名称

Route::get('/theme/{theme-id}/topics/{topic-id}', 'TopicsController@show')->name('showtopic');

然后将两个参数传递为

{{ route('showtopic', ['theme-id' => $theme->id, 'topic-id' => $topic->id]) }}

如果您只想使用一个参数调用路线,请在路线中将第二个参数作为选项

Route::get('/theme/{theme-id}/topics/{topic-id?}', 'TopicsController@show')->name('showtopic');

然后将两个参数传递为

{{ route('showtopic', ['theme-id' => $theme->id]) }}

答案 1 :(得分:0)

要使用route()帮助程序,请将id重命名为:

Route::get('/theme/{themeId}/topics/{topicId}', 'TopicsController@show')->name('showtopic');

因为这条路线有两个参数,你需要传递两个参数:

{{ route('showtopic', ['themeId' => $theme->id, 'topicId' => $topic->id]) }}