I'm using laravel 9 and I'm new to using the blade template view files.
I have a 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 accordingly 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.