我的页面上存在未定义的变量错误。 我正在使用软件包聊天工具,并且正在添加一个字段以向诸如StackOverflow之类的帖子添加评论。 但是,当我路由ChatterReplyController @ show并传递$ chatterreplies变量时,视图页面显示了未定义的变量。
这是控制器的代码
public function show($id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
echo "<pre>"; print_r('$chatterreplies'); die();
// or use the laravel helper
dd($chatterreplies);
return view('chatter::discussion', compact('chatterreplies'));
}
路由文件代码发布组
Route::group([
'as' => 'posts.',
'prefix' => $route('post', 'posts'),
], function () use ($middleware, $authMiddleware) {
// All posts view.
Route::get('/', [
'as' => 'index',
'uses' => 'ChatterPostController@index',
'middleware' => $middleware('post.index'),
]);
// Create post view.
Route::get('create', [
'as' => 'create',
'uses' => 'ChatterPostController@create',
'middleware' => $authMiddleware('post.create'),
]);
// Store post action.
Route::post('/', [
'as' => 'store',
'uses' => 'ChatterPostController@store',
'middleware' => $authMiddleware('post.store'),
]);
//Adding Comments
Route::post('/reply/{id}', [
'as' => 'store',
'uses' => 'ChatterReplyController@store',
'middleware' => $authMiddleware('post.reply.store'),
]);
//showing Comment
Route::get('/reply/{id}', [
'as' => 'show',
'uses' => 'ChatterReplyController@show',
'middleware' => $authMiddleware('post.reply.show'),
]);
这是我在查看页面上显示内容的方式
@foreach($chatterreplies as $chatterreply)
{{$chatterreply->reply}}
@endforeach
答案 0 :(得分:0)
尝试
public function show($id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
echo "<pre>"; print_r('$chatterreplies'); die();
return view('chatter::discussion', compact('chatterreplies'));
}
在您的route
中,您通过了一个parameter