Skip to content
Advertisement

updating blade view everytime theres an update in the the database table

I have a stat bar on my blade, which for example says ‘Calls Today : 0’ etc,

I need for the the stats to update each time theres an update in the sql database for calls today. so for example if calls Today goes up to 5 in the database, the blade views Calls Today must also go up to 5. I’m not quite sure if this would be best done with an ajax call that just refreshes the page or specific div every few minutes or if theres something I can do in php to track the table?

my blade view code is currently using @foreach’s to pass the data in from the controller. I like this method and im hoping the solution would likely be ajax or in the controller?

          @foreach $data as $stat
    <div class="StatBar">
    Calls Today: {{$stat->CallsToday}}
    </div>
  <div class="StatBar">
    Calls Today: {{$stat->HoursWorked}}
    </div>
        @endforeach

Advertisement

Answer

There’s a few ways you could do it. The “easy” and simple way would be to just refresh the page periodically, but that may not be desirable if there are interactive elements on the page (eg. you don’t want to interrupt a user filling in a form).

The harder but “better” way would be to use websockets to notify the browser when this changes, but that requires more complexity on the front-end and a web socket server. The “Broadcasting events” section in the Laravel documentation explains how to go about this: https://laravel.com/docs/8.x/broadcasting#introduction

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement