Answer the question
In order to leave comments, you need to log in
Why does AJAX return 1 Comment 22 times?
I wrote a method for returning comments without reloading the page, it works very strange. I chose 1 comment from the database, multiplied it and displayed it.
public function ListComment(){
$comments = Comment::all() where('comment_id', 0)->get();
return response()->json(['data' => $comments]);
}
commentlist = function(){
$.ajax({
url: 'api/Show',
type: "POST",
success: function(response){
data = response.data;
for($i = 0; $i < response.data.length; $i++ ){
$("#commentshow").append("<div class='col-md-12' style='margin-top:20px;'><div class='card'><div class='card-header'><h3>"+response.data[1].name+"</h3></div><div class='card-body'>"+response.data[1].comment+"</div></div></div>");
}
}
});
public function show($slug)
{
$article = blog_article::where('slug', $slug)->first();
$comments = $article->comments()->where('comment_id', 0)->get();
return view('show', compact('article', 'comments')) ;
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question