aurelo
1 year ago

Why Pagination laravel ignores requests

Posted 1 year ago by aurelo

Hi,

My controller:

public function index(Request $request)
{
    $contractstatus=ContractsStatus::all();

    if (($request->fromdatum == null)||($request->todatum == null)) {
        $value_stav_zakazky =1;
        $contracts = Contract::where('stav_zakazky', '1')
            ->orderBy('id', 'DESC')
            ->paginate(10);   
      
        $from=('2018-06-01');
        $to = date("Y-m-d");
    }
    else
    {
        $value_stav_zakazky =$request->stav_zakazky;
        $from=$request->fromdatum;
        $to=$request->todatum;

        $contracts = Contract::where('stav_zakazky', $request->stav_zakazky)->whereDate('created_at', '>=', $from)
            ->whereDate('created_at', '<=', $to)->orderBy('id', 'DESC')
            ->paginate(10);        

}

My view:

                .....
                <form  method="post" action="{{ route('contracts.index') }}" role="form">
                            {{ csrf_field() }}
                            <input type="hidden" name="_method" value="get">
                            <div class="box-body">
                                <label>Časové obdobie od-do:</label>
                                <div class='input-group date' id='datetimepicker1'>
                                    <input name="fromdatum" type='text' value="{{$fromdatum}}" class="form-control" />
                                    <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
                                </div>
                                <div class='input-group date' id='datetimepicker1'>
                                    <input name="todatum" type='text' value="{{$todatum}}" class="form-control" />
                                    <span class="input-group-addon">
                    <span class="glyphicon glyphicon-calendar"></span>
                </span>
                                </div>
                                <div class="form-group">
                                    <label>Stav zákazky:</label>
                                    <select id="stavzakazky" name="stav_zakazky"  class="form-control">
                                        @foreach($contractstatus as $contractstatus2)
                                            @if($contractstatus2->id == $value_stav_zakazky)
                                                <option value="{{$contractstatus2->id}}" selected>{{$contractstatus2->nazov_stavu}}</option>
                                            @else
                                                <option value="{{$contractstatus2->id}}">{{$contractstatus2->nazov_stavu}}</option>
                                            @endif
                                        @endforeach

                                    </select>
                                </div>
                                <button type="submit" class="btn btn-primary">Zobraziť</button>
                            </div>

                            <!-- /.box-body -->
                        </form>
                                .....
                              @foreach ($contracts as $contract)   

                                    <tr>
                                        <td><a style="color: red" href="/contracts/{{ $contract->id }}">  {{ $contract->poradove_cislo}}</a></td>
                                        <td><a style="color: red" href="/contracts/{{ $contract->id }}">  {{ $contract->nazov }}</a></td>
                                        <td><span style="color: red">{{$contract->zodpovednaosoba->meno}}</span></td>
                                        <td><span style="color: red">{{$contract->stavzakazky->nazov_stavu}}</span></td>
                                        <td><span style="color: red">{{$contract->klientzakazky->nazov_firmy}}</span></td>
                                        <td><span style="color: red">{{$contract->pocet_ks}}</span></td>
                                        <td><span style="color: red">{{$contract->created_at->format('d.m.Y')}}</span></td>
                                    </tr>

                            @endforeach

                        </table>
                        {{ $contracts->links() }}

When I click on next (other) pages so Reguest is null and I will see default values.

Please sign in or create an account to participate in this conversation.