user11710915
user11710915

Reputation: 434

How to loop Ajax response in blade

I'm fetching data using Ajax from the database now I want to loop the response I get from the database, but I'm stuck, have no idea on how I can loop this. Any help would be appreciated.

//This is how I loop using Laravel(I want to loop like this with Ajax)

@foreach(range(1,5) as $i)
@if($total >0)
@if($total >0.5)
 <div>Full star</div>
@else
<div >Half star</div>
@endif
@else
 <div >Empty star</div>
@endif
<?php $total--; ?>
@endforeach

//This is the response from Ajax which displays total number from database

<span id="mycount"></span> 

Ajax

function getCount() {
$.ajax({
type: "GET",
url: '{{route('live.review')}}',
success: function(data) {
   $('#mycount').html(data);
   setTimeout(getCount, 1000);
}
});

}
getCount();

Upvotes: 3

Views: 1612

Answers (1)

dsat
dsat

Reputation: 147

You cannot use Blade to loop through an Ajax request. Blade templates are rendered server-side and jQuery runs on the clients browser. You have to use javascript to do this.

One way to do this would be:

success: function(data) {
   for(item of data){
     $('#mycount').append(`<div>${item}</div>`)
   }
}

I have no idea what you are trying to achieve or what type of data you are getting but this is something to get you started.

Upvotes: 1

Related Questions