update a laravel real-time datatable

1

helpme                 Name

   <th scope="col">Nombre proyecto</th>
 <th scope="col">Aprobado </th>
  <th scope="col">No Aprobado</th>
   <th scope="col">Abstencion</th>
</tr>

  

@foreach ($mostrarusers as $mostraruser)
<tr>


  <td>{{$mostraruser->usuario}}</td>

    <td>{{$mostraruser->proyecto}}</td>
    <td>{{$mostraruser->si}}</td>
    <td>{{$mostraruser->no}}</td>
     <td>{{$mostraruser->voto_en_blanco}}</td>

</tr>
  @endforeach

the idea is that the table is shown real time

the controller function is this

$mostrarusers = DB::table('votos')
->join('users','users.id','=','votos.user_id')

->join('nombre_votos','nombre_votos.id','=','votos.id_nombre_votos')
->select('users.name as usuario', 'nombre_votos.nombre as 
proyecto','si','no','voto_en_blanco')
->whereDate('votos.created_at', Carbon::today())

->get();
    
asked by Ramon Albares 06.11.2018 в 16:58
source

1 answer

0

Correct me if I'm wrong (since you did not send a lot of data to understand you better), but I suppose you have something like that in your controller:

function controladorVista()
{
 $mostrarusers = DB::table('votos')
  ->join('users','users.id','=','votos.user_id')
  ->join('nombre_votos','nombre_votos.id','=','votos.id_nombre_votos')
  ->select('users.name as usuario', 'nombre_votos.nombre as 
   proyecto','si','no','voto_en_blanco')
  ->whereDate('votos.created_at', Carbon::today())
  ->get()
  return view('index', compact('mostrarusers'));
}

If so, I regret to say that your list will only be updated when you refresh (reload the page). Now starting from that the options that you have to update your page without needing to reload this would be:

1 - Make a request via ajax to a path / endpoint of your server in laravel, this will bring you your list and you will be able to update your table, for that you would have to add the function setInterval of js that executes it from time to time (such every 3 minutes).

On your controller:

function getMostrarUsers()
    {
        $mostrarusers = DB::table('votos')
          ->join('users','users.id','=','votos.user_id')
          ->join('nombre_votos','nombre_votos.id','=','votos.id_nombre_votos')
          ->select('users.name as usuario', 'nombre_votos.nombre as 
           proyecto','si','no','voto_en_blanco')
          ->whereDate('votos.created_at', Carbon::today())
          ->get();
        return response()->json($mostrarusers, 200);
    }

In your routes file (routes / web.php) by default

Route::get('/mostrar-users', 'TuControlador@getMostrarUsers')->name('rest.getMostrarUsers');

(In your view / view) Then in the part of your <scripts> or a file js external%, it would be more or less like this (Using JQuery ):

function cargarTabla(){
        $.ajax({
            url     : '{{ route('rest.getMostrarUsers')}}',
            method  : 'GET',
            success : function(r){
                let lista = r;
                let htmlCode = '';
                $.each(lista, function(index, item){
                    htmlCode+='<tr>
                                <td>${item.nombreProyecto}</td>
                                <td>${totalDeSi}</td>
                                <td>${totalDeNo}</td>
                                <td>${totalVotoBlanco}</td>
                            </tr>';
                });
                $('#id-mi-tabla tbody').html(htmlCode);
            }
        });
    }

In the same document using the document ready, you would load this function every so often:

$( document ).ready(function() {
        setInterval(cargarTabla, 180000);//Cada 3 minutos (180 mil milisegundos)
    });

2 - Use websockets, which would be optimal, with this you could configure that every time you do an insert / update / delete something related to your list (like a vote), I shot an event in laravel that is responsible for updating your table to all customers who subscribe to this. this would be in real time.

    
answered by 09.11.2018 в 08:36