LARAVEL - 无法在控制器方法中调用函数

LARAVEL - Not able to call a function inside a controller method

这里是CommentController的注释方法

public function comment(Request $request)
{

    $comments = DB::table('comments')
                    ->join('answers', 'answers.id' , '=', 'comments.answer_id')
                    ->join('users' , 'users.id' , '=', 'comments.user_id')
                    ->where('answers.id', '=' , '9')
                    ->where('parent_id', '0')
                    ->select('comments.comment as comment',
                             'comments.id as comment_id',
                             'comments.created_at as created_at',
                             'comments.parent_id as parent_id',
                             // 'answers.aanswer as answer',
                             'answers.id as ans_id')
                    ->orderBy('created_at', 'desc')
                    ->get();


    foreach ($comments as $comment) {
         echo $comment->comment_id.$comment->comment.'<br>';
        return $this->testingComment();
    }


    public function testingComment(){
        echo "testing comments function";
    }

}

我有这个 foreach 循环,我只是想在循环中调用这个 testingComment 函数,但它不起作用。

我正在构建一个嵌套的评论系统,我想在 foreach 循环中调用函数以在 parent_id 匹配

时呈现子子评论和子子评论

请复制并粘贴下面的代码,它可以正常工作,您遗漏了一些 }:

public function comment(Request $request)
{

      $comments = DB::table('comments')
                        ->join('answers', 'answers.id' , '=', 'comments.answer_id')
                        ->join('users' , 'users.id' , '=', 'comments.user_id')
                        ->where('answers.id', '=' , '9')
                        ->where('parent_id', '0')
                        ->select('comments.comment as comment',
                                'comments.id as comment_id',
                                'comments.created_at as created_at',
                                'comments.parent_id as parent_id',
                                // 'answers.aanswer as answer',
                                'answers.id as ans_id')

                        ->orderBy('created_at', 'desc')
                        ->get();


       foreach ($comments as $comment) {
          echo $comment->comment_id.$comment->comment.'<br>';
          return $this->testingComment();
       }

}

public function testingComment(){
                echo "testing comments function";

}

因为你在函数内部定义函数,试试下面的代码

public 函数注释(请求 $request){

    $comments = DB::table('comments')
            ->join('answers', 'answers.id', '=', 'comments.answer_id')
            ->join('users', 'users.id', '=', 'comments.user_id')
            ->where('answers.id', '=', '9')
            ->where('parent_id', '0')
            ->select('comments.comment as comment', 'comments.id as comment_id', 'comments.created_at as created_at', 'comments.parent_id as parent_id',
                    // 'answers.aanswer as answer',
                    'answers.id as ans_id')
            ->orderBy('created_at', 'desc')
            ->get();


    foreach ($comments as $comment) {
        echo $comment->comment_id . $comment->comment . '<br>';
        return $this->testingComment();
    }
}

public function testingComment() {
    echo "testing comments function";
}

您的其他功能在评论功能中,这很可能是它没有做任何事情或中断的原因。

public function comment(Request $request)
{
    $comments = DB::table('comments')
                    ->join('answers', 'answers.id' , '=', 'comments.answer_id')
                    ->join('users' , 'users.id' , '=', 'comments.user_id')
                    ->where('answers.id', '=' , '9')
                    ->where('parent_id', '0')
                    ->select('comments.comment as comment',
                             'comments.id as comment_id',
                             'comments.created_at as created_at',
                             'comments.parent_id as parent_id',
                             // 'answers.aanswer as answer',
                             'answers.id as ans_id')
                    ->orderBy('created_at', 'desc')
                    ->get();


    foreach ($comments as $comment) {
         echo $comment->comment_id.$comment->comment.'<br>';
        return $this->testingComment();
    }
}

public function testingComment()
{
    echo "testing comments function";
}