私はフォーラムを作っています。ユーザーがテーマをクリックすると、そのテーマに属するすべてのトピックがあるページにリダイレクトされます。私がしようとしているのは、ユーザーがトピックをクリックしてトピックにリダイレクトされるということです。Laravel 5.2定義されていない変数のテーマ
問題は、私はあなたが以下のコードで見ることができるように、私はテーマ彼/彼女に属しているすべてのトピックをループし、私のtheme.blade.php
でforeachループを持っているUndefined variable: theme
を言ってエラーが出るということですクリックした。ループの始めに、彼/彼女がクリックしたトピックにユーザを連れて来ると思われる<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);
}