【问题标题】:Showing data in Laravel Chatter Package is showing Undefined Variable Error在 Laravel Chatter 包中显示数据显示未定义变量错误
【发布时间】:2018-08-29 18:52:15
【问题描述】:

我正在使用 devdojo 的 chatter 讨论包添加类似 stackoverflow 的评论,所以这是我正在编写代码以显示 cmets 但出现未定义变量错误的问题。 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'),
    ]);

【问题讨论】:

    标签: php laravel package laravel-5.6


    【解决方案1】:

    首先,我建议将调试语句 (...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 而不是 ChatterreplyController@show(是 Chatter 中的 R 字母回复Controller@show capital or not ? ) 如果您遵循 ChatterPostController@store 中的 camelCase 约定。

    【讨论】:

    • 我做了改变,但它仍然说未定义的变量。
    • 我所做的是创建了一个模型和控制器,我想在我安装在项目中的 chatter 包中添加一个新模块,因此我将控制器和模型移到了供应商/devdojo放置其他文件的文件夹。
    猜你喜欢
    • 1970-01-01
    • 2016-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-17
    相关资源
    最近更新 更多