Laravel + AJAX Jquery

Asked

Viewed 120 times

0

Good night,

I’m learning Laravel and need to upgrade a div every 10 seconds.

I have a route that when called makes a query to the database. This route returns the Dashboard view.

Route:

Route::get('/dashboard','DashboardController@buscarMmDia')->name('buscarMmDia');

In the view it processes the select and shows on the screen:

<?php foreach ($buscarMmDia as $p): ?>

        <div class="col s12 m3" id ="frase">
            <div class="card-panel blue">
                <div class="card-content center">
                    <img src="img/weather-rainy2.png">
                    <h5 class="white-text lighten-4">                         
                     <?= $p->mm ?>
                    </h5>
                    <p class="white-text lighten-4">Acumulados hoje.</p>
                </div>
            </div>
        </div>
<?php  endforeach ?>

Controller:

 public function buscarMmDia(){

     $buscarMmDia = DB::table('data_collect')

        ->select(DB::raw('count(*) as mm'))
        ->whereBetween('date_hour_col',array('2018-11-02 00:00:00','2018-11-02 23:59:59'))
        ->get();     


    return view('dashboard')->with('buscarMmDia', $buscarMmDia);

I need this div updated every 10 seconds. That is, select again every 10 seconds.

What’s the right way to do it ?

  • Update if there are changes in the base, you may need to work with gold technician type

  • Use setInterval or setTimeout to access the route every 10 seconds and recover the data... With jquery you update.

1 answer

0

I managed to solve by calling the controller every 5 seconds:

<script>
  $(document).ready(function() {
  setInterval(function() {
  $('#buscaMmDia').load('{{ action('DashboardController@buscarMmDiaAtualiza') }}');
  }, 5000);
  });
</script>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.