在laravel 5.3中通过ajax从数据库中获取帖子的评论

时间:2017-04-02 05:27:10

标签: jquery ajax laravel laravel-5.3

<script>
    var token = '{{ Session::token() }}';
    var commentUrl = '{{ route('comments.store')  }}';
    var fetchComments = '{{ route('commentT')  }}';

</script>  

获取评论的“发布”路线

Route::post('comm', [
                'uses' => 'CommentController@comment',
                'as'   => 'commentT'
            ]);

具有ajax的函数

function getCommentsFromDb(postid){

    console.log("Post"+postid);
    $.ajax({

        type : 'POST',
        url  : fetchComments,
        data : { postid: postid,_token: token },
        success : function(result){
            console.log(result);
        }

    });

}

我正在尝试获取单个帖子的评论,而我正在传递帖子

的ID
 public function comment(Request $request)
    {
        //

            $answer_id = $request['answerid'];

            // echo "controller side ".$test;

            $comments = DB::table('posts')
                        ->join('posts', 'posts.id' , '=', 'comments.answer_id')
                        ->join('users' , 'users.id' , '=', 'comments.user_id')
                        ->where('posts.id', '=' , $post_id)
                        ->select('comments.comment as comment',
                                'comments.created_at as created_at',
                                'users.first_name as first_name',
                                'users.last_name as last_name',
                                'posts.post as post')
                        ->orderBy('created_at', 'desc')
                        ->get();

                   $comm = json_decode($comments);

                   print_r($comm);

        *// I though here i have to write html content which i want to render on the view* 


    }

我在控制台得到了预期的结果,但是如何将这个结果渲染到我的视图中,如果我在核心php中这很简单但是在laravel中我被卡住了

将来我会使用ajax删除,编辑评论功能

以下是我在mys console中获得的内容

Array
(
    [0] => stdClass Object
        (
            [comment] => sdfasdf shameem
            [created_at] => 2017-03-31 21:44:22
            [first_name] => Shameem
            [last_name] => Ansari
            [post]  => fist post content fist post content fist post content fist post content fist post content fist post content fist post content fist post content 
        )

    [1] => stdClass Object
        (
            [comment] => game on
            [created_at] => 2017-03-31 21:22:44
            [first_name] => Waqaar
            [last_name] => Aslam
            [post]    => fist post content fist post content fist post content fist post content fist post content fist post content fist post content fist post content
        )

)

我假设首先我必须将我在控制台中获得的这些值放在html结构中,因为我想要评论部分格式,然后我可以这样做#div_to_show

1 个答案:

答案 0 :(得分:1)

我想你想在页面中显示评论。

  1. 您可以使用GET方法而不是POST方法。 (当您获取数据时,您不会提交任何POST数据。)

  2. 取代console.log(result);或低于console.log(result);,而不是('#div_to_show').html(result);,其中div_to_show是div标记的ID。

  3. 您可以返回JSON数据并使用jQuery附加它们。 (好主意,如果你想使用Ajax。)

  4. 代码:

    function getCommentsFromDb(postid){
        console.log("Post"+postid);
        $.ajax({
    
            type : 'POST',
            url  : fetchComments,
            data : { postid: postid,_token: token },
            success : function(result){
                console.log(result);
                $('#div_to_show').html(result); // If it is json try      $.each(result, function(i, field){$("#div_to_show").append(field + " ");});
            }
        });
    }
    

    php代码:

     public function comment(Request $request)
        {
            //
    
                $answer_id = $request['answerid'];
    
                // echo "controller side ".$test;
    
                $comments = DB::table('posts')
                            ->join('posts', 'posts.id' , '=', 'comments.answer_id')
                            ->join('users' , 'users.id' , '=', 'comments.user_id')
                            ->where('posts.id', '=' , $post_id)
                            ->select('comments.comment as comment',
                                    'comments.created_at as created_at',
                                    'users.first_name as first_name',
                                    'users.last_name as last_name',
                                    'posts.post as post')
                            ->orderBy('created_at', 'desc')
                            ->get();
    
                       $comms = json_decode($comments);
    
                       //Instead of print_r()
                       foreach($comments as $comment) {
                           echo "<div>";
                           echo $comment->first_name;
                           echo $comment->last_name;
                           echo $comment->comment;
                           echo $comment->created_at;
                           echo "</div>";
                       }
        }