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

时间:2017-04-16 06:39:47

标签: php mysql laravel laravel-5.2 laravel-5.3

这是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匹配时呈现子和子子注释

3 个答案:

答案 0 :(得分:0)

请复制并粘贴下面的代码,它会正常工作,你错过了一些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"; }

    $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";
}

答案 1 :(得分:0)

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

公共功能评论(请求$请求){

jsonStore=('/storage/emulated/0/hello.json')

答案 2 :(得分:0)

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

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";
}