Search code examples
phplaraveleloquentlaravel-livewire

Make eloquent Collection in div in real-time wihout an active trigger


I have a page that only gives users real-time browsing information. I hope that the data in it can be updated automatically without refreshing the page, because users will not operate events such as button presses, form sending, etc. on this page. , the data only enters the database from other places.

I can think of several situations where automatic updates can be made:

  1. The livewire of php can be bound to the HTML element to update the data synchronously, but because this page will not have active events, I would like to ask whether the livewire function can work in my condition?

  2. I know that JS's setInterval(function(){} can automatically repeat operations. Is this the simplest and crudest method?

  3. I heard the query to use websocket to detect whether the database table is updated, but I need to know the specific method of monitoring the database and interacting with js

Does anyone know a workaround or what keywords should I use to look up the relevant aprroach?

@for ($i = 0; $i < count($DataSet); $i++)
<div class="container" id="container">
<div> {{$DataSet[$i]->real-time info}} </div>
<div> {{$DataSet[$i]->real-time number}} </div>
</div>
@endfor

Solution

  • Livewire offers a directive called wire:poll that, when added to an element, will refresh the component every 2s.

    https://laravel-livewire.com/docs/2.x/polling