我正在使用devdojo的chatter讨论包来添加诸如stackoverflow之类的注释,因此这是我正在编写代码以显示注释的问题,但是出现未定义的变量错误。 Error Page ScreenShot
public function show(Chatterreply $chatterreply ,$id)
{
$chatterreplies = Chatterreply::where('chatter_post_id',$id)->get();
return view('chatter::discussion', compact('chatterreplies'));
echo "<pre>"; print_r('$chatterreplies'); die;
}
在Web.php中,路由是
/*
* Post routes.
*/
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' => $middleware('post.show'),
]);
答案 0 :(得分:0)
首先,我建议通过以下方式将调试语句(... print_r ...)放在控制器动作的return语句之前:
public function show(Chatterreply $chatterreply ,$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'));
}
您应该看到$ chatterreplies变量的内容。
如果可以,请在web.php中检查您的控制器名称,因为它似乎应该是ChatterReplyController @ show而不是Chatter reply Controller @ show(是Chatter中的R字母 是否要回复 Controller @是否显示大写字母?)(例如,如果您遵循的是camelCase约定,例如在ChatterPostController @ store中。)