2017-06-17 46 views
0

我正在創建一個論壇,如果用戶點擊一個主題,他會被重定向到所有主題屬於該主題的頁面。我想要做的是,用戶可以點擊該主題並將其重定向到該主題。Laravel 5.2未定義的可變主題

的問題是,是,我得到一個錯誤說Undefined variable: theme

正如你可以在下面的代碼中看到的,我有我的theme.blade.php foreach循環,其循環屬於主題的所有主題的他/她點擊。在循環的開始處有一個<a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}"1,它應該讓用戶看到他/她點擊的主題。應該顯示的URL是forum.dev/theme/{theme_id}/topics/{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> 
      </div> 
     </div> 

Web.php

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


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


Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function() { 

//THEMES 

Route::get('/theme/{theme_id}/edit', '[email protected]')->name('edittheme'); 
Route::patch('/theme/{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/{theme_id}/delete', '[email protected]')->name('deletetheme'); 

//TOPICS 

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

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

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

}); 

Route::get('user/profile', '[email protected]')->name('showprofile'); 
Route::post('user/profile', '[email protected]_avatar'); 

TopicsController.php(顯示方法)

public function show($id) 
{ 
    $theme = Theme::find($id); 
    $topics = Topic::find($id); 

    return view('topics/topic')->with('topics', $topics)->with('theme', $theme); 
} 

回答

0

我不知道你是否可以使用雙' - >'選項。

什麼工作如下:

return view('topics/topic', ['topics' => $topics, 'theme'=> $theme]); 

你傳遞一個數組作爲視圖的第二個參數。您可以根據需要添加儘可能多的鍵和變量。