2017-06-17 65 views
3

我正在製作一個論壇,主題和主題。如果用戶點擊主題,他/她會看到該主題中的所有主題。這裏我們遇到第一個問題。在theme.blade.php我有一個標題:<span class="card-title">{{ $theme->theme_title }} - Topics</span>。這個標題應該顯示用戶點擊的主題的標題。但它顯示了(只是一種瘋狂的猜測)從數據庫中的一些隨機主題標題,甚至沒有連接到這個主題。Laravel查看鏈接和web.php路線搞砸了

現在我爲用戶提供了額外的視圖。如果用戶點擊所選主題中的主題。他/她應該重定向到他/她點擊的主題,而是它會再次顯示數據庫中的一些隨機主題,而這些主題根本沒有連接到主題/主題。而不是用戶點擊的主題。在此GIF http://imgur.com/a/vOQFT中,您可以看到問題如果您查看個人資料照片和用戶名。也許問題出在Web.php或其他地方,我不知道。對不起,長話短說,但我無法弄清楚如何以更好的方式說出這些。我想我在代碼中切換了一些東西。

這裏是代碼的每個文件都可能發生

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'); 

Theme.blade.php(每一個主題的主題範圍內的清單)

<div class="col s12"> 
      <div class="card"> 
       <div class="card-content"><span class="card-title">{{ $theme->theme_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="card-title">{{ $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> 

ThemesController.php(僅顯示方法)

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

    return view('themes.theme')->with('topics', $topics)->with('theme', $theme); 
} 

TopicsController.php(只顯示法)

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

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

} 

感謝您看我的代碼。這個問題在這裏已經有一段時間了,我想繼續前進。謝謝你的幫助!

+0

使用相同的'$ id'加載'Theme'和'Topic'。這看起來不正確。 – tkausl

+0

它說'Theme :: find($ id)'這只是找到了主題的ID吧?所以如果我用'Topics :: find($ id)'來表示另一個人,那麼只需找到該主題的ID即可。那些不介入,對吧? –

+0

不,它完全相反。它找到ID爲'$ id'的主題。我懷疑標識爲'$ id'的主題和標識爲'$ id'的主題是否屬於同一個。 – tkausl

回答

0

您的控制器代碼只需找到ID爲$id的主題以及ID爲$id的主題(單數!)。該特定主題可能不會出現在該特定主題中。他們可能沒有任何關係。

要找到屬於與ID $id主題的主題,你可以這樣做:

$theme = Theme::find($id)->with('topics'); 

(這裏假設你的模型關係的設置是否正確,你還沒有告訴我們的)。 See the docs on eager loading

要訪問的主題在你看來,做這樣的事情:

@foreach ($theme->topics as $topic) 
    ... 
    {{ $topic->user->username }} 
    ... 

在開發,你可以簡單地

return $theme; 
在你的控制器

看到數據的結構,所以你可以計算出如何處理和迭代它。