【发布时间】:2017-06-17 16:53:48
【问题描述】:
我正在创建一个有主题和主题的论坛。如果用户点击一个主题,他/她可以看到该主题中的所有主题。这里我们遇到了第一个问题。在theme.blade.php 我有一个标题:<span class="card-title">{{ $theme->theme_title }} - Topics</span>。这个标题应该显示用户点击的主题的标题。但它显示(只是一个疯狂的猜测)来自数据库的一些随机主题标题,甚至没有连接到这个主题。
现在我为用户制作了一个额外的视图。如果用户单击所选主题中的主题。他/她应该重定向到他/她点击的主题,但它会(再次)显示来自数据库的一些随机主题,这些主题根本没有连接到主题/主题。而不是用户点击的主题。在这个 GIF http://imgur.com/a/vOQFT 你可以看到问题如果你看一下个人资料图片和用户名。也许问题出在Web.php 或其他地方,我不知道。很抱歉这个长篇大论,但我无法弄清楚如何以更好的方式说出来。我想我在代码中改变了一些东西。
这是可能出现此问题的每个代码文件
Web.php
Route::get('/', 'ThemesController@index')->name('home');
Route::get('/theme/{theme_id}/topics', 'ThemesController@show')->name('showtheme');
Route::get('/theme/{theme_id}/topics/{topic_id}', 'TopicsController@show')->name('showtopic');
Route::group(['middleware' => 'App\Http\Middleware\AdminMiddleware'], function() {
//THEMES
Route::get('/theme/{theme_id}/edit', 'ThemesController@edit')->name('edittheme');
Route::patch('/theme/{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/{theme_id}/delete', 'ThemesController@destroy')->name('deletetheme');
//TOPICS
Route::get('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController@edit')->name('edittopic');
Route::patch('/theme/{theme_id}/topics/{topic_id}/edit', 'TopicsController@update')->name('updatetopic');
Route::get('/theme/{theme_id}/topics/create', 'TopicsController@create')->name('createtopic');
Route::post('/theme/{theme_id}/topics/create', 'TopicsController@save')->name('savetopic');
Route::delete('/theme/{theme_id}/topics/{topic_id}/delete', 'TopicsController@destroy')->name('deletetopic');
});
Route::get('user/profile', 'UserController@profile')->name('showprofile');
Route::post('user/profile', 'UserController@update_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);
}
感谢您查看我的代码。这个问题已经存在了很长一段时间,我想继续前进。感谢您的帮助!
【问题讨论】:
-
您使用相同的
$id加载Theme和Topic。这看起来不对。 -
它说
Theme::find($id)这只是找到主题的ID 对吗?因此,如果我将另一个与Topics::find($id)放在一起,则只需找到该主题的ID。那些不干预,对吧? -
不,恰恰相反。它会找到 ID 为
$id的主题。我怀疑 ID 为$id的主题和 ID 为$id的主题会属于一起。 -
两者的关系是:一个主题有很多主题,一个主题属于一个主题。那么我该如何解决呢?将主题的
$id更改为$topic_id?
标签: php laravel laravel-5 laravel-5.2