@darrion.kuhn
To auto refresh a div every x seconds with Laravel, you can use JavaScript and AJAX to periodically reload the content of the div. Here is a basic example:
- In your Blade template, add a div with an id that you want to refresh:
- Add a JavaScript function to make an AJAX request and update the content of the div:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<script>
function refreshDiv() {
$.ajax({
url: '/refresh',
type: 'GET',
success: function(response) {
$('#myDiv').html(response);
}
});
}
$(document).ready(function() {
refreshDiv();
setInterval(refreshDiv, 5000); // Refresh every 5 seconds
});
</script>
|
- Create a route in your Laravel routes file to handle the AJAX request:
1
2
3
4
|
Route::get('/refresh', function() {
$data = // fetch the data you want to display in the div
return view('partials.myDiv')->with('data', $data);
});
|
- Create a partial view file for the content you want to display in the div (e.g., resources/views/partials/myDiv.blade.php):
Now, every 5 seconds, the myDiv
div will be refreshed with the data fetched from your Laravel application.