Refresh php blade foreach loop from jQuery
To refresh a Blade view's @foreach
loop dynamically using jQuery, you can use AJAX to fetch the latest data from the server and update the HTML content accordingly. Here is a step-by-step guide to achieve this:
Step 1: Set up the Laravel Project
Ensure you have a Laravel project set up and configured to connect to your database as explained previously.
Step 2: Create a Route and Controller Method for AJAX
Create a route in
routes/web.php
:phpuse App\Http\Controllers\DataController; Route::get('/data', [DataController::class, 'index']); Route::get('/fetch-data', [DataController::class, 'fetchData']);
Update your controller to handle the AJAX request in
app/Http/Controllers/DataController.php
:phpnamespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Data; class DataController extends Controller { public function index() { $data = Data::all(); return view('data.index', compact('data')); } public function fetchData() { $data = Data::all(); return response()->json($data); } }
Step 3: Create the Blade View
Create a Blade view in
resources/views/data/index.blade.php
:html<!DOCTYPE html> <html> <head> <title>Live Data</title> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> </head> <body> <div class="container mt-5"> <h1>Live Data</h1> <table class="table table-bordered"> <thead> <tr> <th>Name</th> <th>Value</th> </tr> </thead> <tbody id="data-table-body"> @foreach($data as $item) <tr> <td>{{ $item->name }}</td> <td>{{ $item->value }}</td> </tr> @endforeach </tbody> </table> </div> <script> $(document).ready(function() { function fetchData() { $.ajax({ url: '/fetch-data', method: 'GET', success: function(data) { let tableBody = $('#data-table-body'); tableBody.empty(); data.forEach(item => { tableBody.append(`<tr><td>${item.name}</td><td>${item.value}</td></tr>`); }); } }); } setInterval(fetchData, 5000); // Refresh every 5 seconds }); </script> </body> </html>
Explanation
- Route and Controller: The route
/fetch-data
is defined to handle AJAX requests and returns the latest data in JSON format. - Blade View: The Blade view (
index.blade.php
) contains a table where the data will be displayed. It also includes jQuery to handle the AJAX request. - jQuery Script:
- fetchData Function: This function sends an AJAX GET request to the
/fetch-data
endpoint, receives the latest data in JSON format, and updates the table body with the new data. - setInterval: This function calls
fetchData
every 5 seconds to refresh the data.
- fetchData Function: This function sends an AJAX GET request to the
Step 4: Test the Setup
Run your Laravel development server:
shphp artisan serve
Navigate to the page (e.g.,
http://localhost:8000/data
). You should see the data in the table being refreshed every 5 seconds with the latest data from the database.
Conclusion
This setup allows you to refresh a Blade view's @foreach
loop dynamically using jQuery. The AJAX call fetches the latest data from the server at regular intervals and updates the HTML content without needing a full page reload.
Post a Comment