How can I pull data in real time from a MySQL database via Laravel?
I'd prefer to call the code from a blade view.
How can I implement this, or what's the right approach for this?
The line of code I'm using to get the data is:
Facility::where('aID',$this->id)->where('type',$type)->count();
As I'm sure you're aware, with pure PHP you won't be able to do this.
If you're open to "live" meaning, periodically every second or so, you could do this with a recurring ajax call with jQuery which would do a recount, and replace the current count with the new value.
It will mean also, that you won't be able to do this from a single blade view, you're going to need to setup a controller to be able to do your ajax call to which will return you the new count.
<h1>
The current count is
<span id="mycount">
{{ Facility::where('aID',$this->id)->where('type',$type)->count(); }}
</span>
</h1>
function getCount() {
$.ajax({
type: "GET",
url: "{{ route('route_for_new_data_here') }}"
})
.done(function( data ) {
$('#mycount').html(data);
setTimeout(getCount, 1000);
});
}
getCount();
In a controller, you'll need something then to work with this and send back the count
function getCount()
{
echo Facility::where('aID',$this->id)->where('type',$type)->count();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.