How to make dynamic query using select in Laravel

Asked

Viewed 572 times

0

I have in my view a with Dashboard/index.

This index has a table and a chart that by default, it brings information of the current month (first and last day of the month). I would like to update dynamically through select that has information from the past 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 https://imgur.com/a/uTHElI

  • 1

    But refreshing gives the same error ? The controller is being called ? dd($dashboard); gives the result you expect ?

  • Puts your answer that csrf is missing in the form.

1 answer

1


Usually this message is sent when you tried to send a form without protection or has already expired.

How you are doing the filter using the method post form. Obligatorily you need to enter Laravel’s 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>

Browser other questions tagged

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