Oops
Oops

Reputation: 1413

Display JSON result in table Laravel 5.8

I want to display the JSON results to my HTML table in Laravel 5.8. The result returned from the controller is as follows.

[[{"logged_on":"2019-06-24 00:00:00"},{"logged_on":"2019-06-21 00:00:00"}]]

My Ajax success function is as follows:

 $.ajax({
      url: url,
      type: "get",
      data: {user_id:user_id},
      contentType: "application/json",dataType: "json",
       success: function(data){ // What to do if we succeed
      if(data){
          alert(data);
            var len = data.length;
            var txt = "";
            if(len > 0){
                for(var i=0;i<len;i++){
                    if(data[i].logged_on){
                        txt += "<tr><td>"+data[i].logged_on;
                    }
                }
                if(txt != ""){
                    $("#table").append(txt).removeClass("hidden");
                }
            }
        }

else{
    alert("fails");
}
}
    });

Below is my controller function

$user_id = $requests->input('user_id');
$data = UserLogins::select('logged_on')->where('user_id',$user_id)->get()->toArray();

return response()->json(array($data));

But I am not getting the result in my table.

Upvotes: 0

Views: 90

Answers (1)

Vipertecpro
Vipertecpro

Reputation: 3274

$user_id = $requests->input('user_id');
$data = UserLogins::select('logged_on')->where('user_id',$user_id)->get()->toArray();

return response()->json($data); // remove array() because $data return array

Upvotes: 1

Related Questions