我正在建立一个论坛,如果用户点击主题,他会被重定向到所有主题属于该主题的页面。我正在尝试做的是,用户可以点击该主题并重定向到该主题。
问题是,我收到错误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('/', 'ThemesController@index')->name('home');
Route::get('/theme/{theme_id}/topics', 'ThemesController@show')->name('showtheme');
Route::get('/theme/{theme_id}/topics/{topic_id}', 'TopicsController@topic')->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');
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 :(得分:0)
我不知道你是否可以使用&#39; - &gt;和&#39;选项。
以下内容将起作用:
<xsl:key name="ref" match="trans-unit" use="@id"/>
您将数组作为视图的第二个参数传递。您可以根据需要添加任意数量的键和变量。