2017-06-17 59 views
2

我不斷收到以下錯誤:Laravel 5.2缺少所需參數[路線:showtopic] [URI:主題/ {ID} /主題/ {ID}]

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

這是我的網站.PHP的樣子:在web.php

有關的一切話題

Route::get('/theme/{id}/topics/{id}', '[email protected]')->name('showtopic'); 

Route::get('/theme/{id}/topics/{id}/edit', '[email protected]')->name('edittopic'); 
Route::patch('/theme/{id}/topics/{id}/edit', '[email protected]')->name('updatetopic'); 

Route::get('/theme/{id}/topics/create', '[email protected]')->name('createtopic'); 
Route::post('/theme/{id}/topics/create', '[email protected]')->name('savetopic'); 

Route::delete('/theme/{id}/topics/{id}/delete', '[email protected]')->name('deletetopic'); 

一切方面ing主題web.php

Route::get('/theme/{id}/topics', '[email protected]')->name('showtheme'); 

Route::get('/theme/{id}/edit', '[email protected]')->name('edittheme'); 
Route::patch('/theme/{id}/edit', '[email protected]')->name('updatetheme'); 

Route::get('/theme/create', '[email protected]')->name('createtheme'); 
Route::post('/theme/create', '[email protected]')->name('savetheme'); 

Route::delete('/theme/{id}/delete', '[email protected]')->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> 

回答

0

要使用route()幫手,重命名id到:

Route::get('/theme/{themeId}/topics/{topicId}', '[email protected]')->name('showtopic'); 

而且因爲這條路線有兩個參數,你需要傳遞兩個參數:

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

我認爲你的答案有效,但是它給我顯示了一個錯誤, 'Undefined variable:theme'因爲$主題沒有在這個刀片模板中定義。我該如何解決這個問題?我用theme.blade.php –

+0

@ feudelcosine148更新了我的問題,將它更改爲帶有主題ID的問題。如果你還沒有它,你應該得到它並傳遞到視圖。 –

+0

你能告訴我一個例子嗎?我不太明白你的意思 –

1

你必須爲你的路線參數給出了兩個不同的參數名稱

Route::get('/theme/{theme-id}/topics/{topic-id}', '[email protected]')->name('showtopic'); 

,然後通過這兩個參數

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

如果你想打電話給你的路線,只有一個參數,然後進行在路線的選擇,因爲

Route::get('/theme/{theme-id}/topics/{topic-id?}', '[email protected]')->name('showtopic'); 

,然後第二個參數傳遞兩個參數

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

它給了我主題中的$主題未定義的錯誤.blade.php 你能告訴我如何解決這個問題嗎?我更新了我的問題,所以現在在調用你的路由時,blade.php在那裏 –

+0

@ feudelcosine148,你沒有傳遞'theme-id'。因此,如何在刀片中獲得它,更好地首先檢查變量是否存在,然後僅在刀片中使用 –

+0

我希望這能說清楚。 HTTP:// imgur。com/a/sgoWj 現在這是我的web.php:http://imgur.com/a/NQVu0 –