2017-06-17 67 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; 
在你的控制器

看到数据的结构,所以你可以计算出如何处理和迭代它。