John Davidson

javascript - Using Carbon in html response frm ajax

0 comments
Message:


In the below code, I want to show updated some hours ago since the data is updated, but ' + $item.updated_at->diffInHours(now()) + ' giving following error Uncaught SyntaxError: expected expression, got '>' It is pointing at -> operator


Any Suggestion is appreciated, Thanks


index.blade.php


<script>
...

function fetchAllNames() {
var _url = '{{ route("names.fetch.route") }}';
$.ajax({
type: "GET",
url: _url,
dataType: "json",
success: function(response) {
$('tbody').html("");
$.each(response.name, function($key, $item) {
$('tbody').append('<tr>\
<td><input type="checkbox" id="sub_master" data-id="' + $item.id + '"></td>\
<td>' + $key + '</td>\
<td>' + $item.name + '</td>\
<td><label id="timeLabel">' + $item.updated_at->diffInHours(now()) + '</label></td>\
\</tr>');
});
}
});
}

...
</script>

CustomController.php


public function FetchName() 
{
$name = CandidateName::all();
return response()->json(['name'=>$name]);
}


My Answer:
new Date().getTime() < 3600000 ? 'just now' : Carbon::parse($item.updated_at)->diffForHumans()

This code snippet checks if the difference between the current time and the updated time is less than an hour. If it is, it will display 'just now', otherwise it will use the Carbon library to calculate the human-readable difference between the updated time and the current time.

Rate this post

3 of 5 based on 6155 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us