Reputation: 237
I've got a mySQL table with 4 columns.One of them is updated_at() from timestamps(). Anyone got any idea how can i detect when the updated_at column changes and update one certain page with ajax?
I know that a better way to do it is a web socket but for my project is a little overkill.The project is on laravel 5.2
Upvotes: 1
Views: 1851
Reputation: 4795
You can detect when model updated using Model Events
But I have no idea how to refresh html page from your PHP script without web sockets.
EDIT
try
Route::get('checkIfModelUpdated/{id}', function($id) {
$model = YourModelClass::find($id);
$updatedAt = Request::get('updated_at');
return json_encode($model->updated_at->gt($updetedAt));
});
and client code
setInterval(function() {
$.get('/checkIfModelUpdated/'+YOUR_MODEL_ID,
{updated_at: LAST_UPDATED_AT},
function(data) {
if (JSON.parse(data) == true)
// refresh your page
});
}, 1000);
Upvotes: 2
Reputation: 5186
Without web sockets, the only way is to have a Javascript setInterval
running on the background and running ajax calls to your Laravel API. The API would check if any changes and respond with the updates, which will be received by your ajax call back and applied to the page.
Upvotes: 1