I'm using laravel 9 and i'm new to using the blade template view files.
I have an blade template file that shows a table and has some buttons to interact with that.
the table is just a result of select * from some_table
, and i want it to be refreshed every second, i tried to google.. found broadcasting.. ajax... everything seems a bit too complicated for a simple task as this.
so I have a route at web.php
Route::get('/test-page', function () {
return view('test-page');
});
and test-page.blade.php
contains:
<?php
$agents = \Illuminate\Support\Facades\DB::select('select * from agents');
?>
<!DOCTYPE html>
<html>
...
<table>
...
<tbody>
@foreach ($agents as $agent)
<tr>
<td>{{ $agent->id }}</td>
<td> {{ $agent->name }}</td>
...
</tr>
@endforeach
i want $agents
variable to be refreshed every second, and the table display would changed accordantly without refreshing the all page. there must be a simple way to do just that.
i guess i can create a route that will return that data in a json format and return it, i'm sure there is a cool laravel way to resolve this just don't know how or where to start really.
any information regarding this issue would be greatly appreciated.
CodePudding user response:
I see that Broadcasting using Pusher Channels is the easiest and optimized solution because in case you used AJAX you'll consume many resources you don't need. For instance, if you send a request per second without any data modification it causes a load on the server so, I recommend using Broadcasting where we can use event broadcasting to dispatch an event that is received by our application's JavaScript. Once the event is received, we can display the data modification without ever needing to refresh the page.