How to do dynamic query using select in Laravel

0

I have in my view the with dashboard / index.

This index has a table and a graph which, by default, contains information for the current month (first and last day of the month). I would like to dynamically update through select which has information from the last few months.

Route:

Route::get('/dashboard', ['as'=>'dashboard', 'uses'=>'DashboardController@index']);
Route::post('/dashboard', ['as'=>'dashboard.buscapormes', 'uses'=>'DashboardController@buscapormes']);

index.blade.php

<form method="post" action="{{ route('dashboard.buscapormes') }}" >
            <select class="mesCorrente" id="mesCorrente" name="buscapormes">
               <option selected="selected" name="buscapormes" value="{{ $MesAtual }}">
        <?=$Mes."/".date('Y');?>
    </option>

        <option name="buscapormes" value='{{ $MesMenosUmMes }} '>
    <?=$MesMenosUm."/".date('Y');?>
    </option>
        <option name="buscapormes" value='{{ $MesMenosDoisMeses }}'>
    <?=$MesMenosDois."/".date('Y');?>
    </option>
<option name="buscapormes" value='{{ $MesMenosTresMeses }}'>
<?=$MesMenosTres."/".date('Y');?>
</option>
    </select>

        <button type="submit">Enviar</button>
        </form>

DashboardController.php

public function buscapormes(Request $req, $buscapormes)
    {
        //$registros = $req->all();
        //dd($registros);


        $title = 'Inffel OnBoard :: Dashboard';
        $subtitle = 'Dashboard';

        $ClienteID = auth()->user()->cliente_id; 

        $dataInicio = substr($buscapormes, 0, 10);
        $dataFinal = substr($buscapormes, 13, 10);      

        $dashboard=DB::table('MVE')
        ->select(DB::raw('DATA, sum(TOTAL) as TOTAL'))
        ->where('CLIENTE', $ClienteID)
        ->whereBetween('DATA', [$dataInicio, $dataFinal])
        ->groupBy('DATA')
        ->orderBy('DATA')
        ->get();


        return view('dashboard.index', compact('title', 'subtitle', 'dashboard'));
    }

Error Print link

    
asked by anonymous 11.12.2018 / 21:15

1 answer

1

This message is usually thrown when you tried to submit an unprotected or expired form.

How you're doing the filter using the post method of the form. You will need to enter the Laravel CSRF token.

Below is an example of how to use:

<form method="post" action="{{ route('index') }}">
  @csrf <--// isso aqui eh obrigatório no método post.

  <input type="text" name="filtro" />
  <input type="submit" value="Enviar" />
</form>
    
15.12.2018 / 05:05