lundi 23 mars 2020

Displaying data without page refresh Timeout not working Ajax and Laravel

I'm trying to display user name from the database using Ajax and Laravel but the timeout doesn't work as expected. What I wanted was if a user submit data to the database it should fetch and display them without refreshing the page. But now the issue is if a user submit data to the database it doesn't display them until I refresh the page but if I delete the data directly from the database they disappear without refreshing the page. Any one knows why this is happening and how I can fix it? I have tried to add async:true, and async:false, but didn't work.

Html

  <span id="userRatingavatar"></span>

Ajax

<script >
  function userRatingavatar() {
    $.ajax({
        type: "GET",
        url: '',
        success: function(data) {
            $('#userRatingavatar').html(data);
            setTimeout(userRatingavatar, 1000);
         }
      });

  }
 userRatingavatar();
</script>

Controller

  public function userRatingavatar($id){
    {
        $productR = Product::where('id','=',$id)->with('reviews.user')->get();
        foreach ($productR as $product)
        {
            foreach ($product->reviews as $review){
                echo  $review->user->name;
            }
        }
    }
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire