Laravel 5.2 未定义变量主题

Laravel 5.2 Undefined variable theme

我正在制作一个论坛,如果用户点击一个主题,他将被重定向到所有主题都属于该主题的页面。我想要做的是,用户能够点击该主题并被重定向到该主题。

问题是,我收到一条错误消息 Undefined variable: theme

正如您在下面的代码中所看到的,我的 theme.blade.php 中有一个 foreach 循环,它循环属于所点击主题 he/she 的所有主题。在循环的开头有一个 <a href="{{ route('showtopic', ['theme_id' => $theme->id, 'topic_id' => $topic->id]) }}"1 应该将用户带到 he/she 单击的主题。应该显示的 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);
}

我不知道你是否可以使用双'->with'选项。

以下是有效的:

return view('topics/topic', ['topics' => $topics, 'theme'=> $theme]);

您传递一个数组作为视图的第二个参数。您可以根据需要添加任意数量的键和变量。