Laravel filter reset after next page click in pagi

2020-02-07 13:33发布

问题:

I have create table where showing data. I also add few filters to filter data. Using paginate i show 20 records per page. After select filter and click search records in table filter with paginate in first page but as soon as i click next page filters getting reset. How to stop filters getting reset?

Below is my code,

public function index() 
{
    $agos = DB::table('orders')
        ->leftJoin('companies', 'orders.company_id', '=', 'companies.id')
        ->select(DB::raw('orders.id, companies.name, orders.type, orders.data, orders.currency, orders.price, orders.status, DATE_FORMAT(orders.created_at,"%M %d, %Y") as created_at '))
        ->where('orders.merchant', '=', 'agos')
        ->where(function ($query) {
            $status  = Input::has('status') ? Input::get('status') : null;
            $company = Input::has('company') ? Input::get('company') : null;
            $from    = Input::has('from_date') ? Input::get('from_date') : null;
            $to      = Input::has('to_date') ? Input::get('to_date') : null;
            $from    = date("Y-m-d", strtotime($from));
            $to      = date("Y-m-d", strtotime($to));

            if ( isset($status) ) {
                $query->where('orders.status', '=', $status);
            }

            if ( isset($company) ) {
                $query->where('companies.name', '=', $company);
            }

            if ( !empty($from) && !empty($to) ) {
                $query->whereBetween('orders.created_at', [$from, $to]);
            }
        })->orderBy('orders.created_at', 'desc')
        ->paginate(20);

    return $agos;
}

Blade file code,

@extends('layouts.agos')
@section('title', Translator::transSmart('app.Common Clerk(AGOS)', 'Common Clerk(AGOS)'))

@section('styles')
    @parent
    {{ Html::skinForVendor('jquery-textext/all.css') }}
@endsection

@section('scripts')
    @parent
    {{ Html::skinForVendor('jquery-textext/all.js') }}

@endsection

@section('content')

    <div class="admin-managing-member-index">

        <div class="row">
            <div class="col-sm-12">

                {{ Form::open(array('route' => array('agos::index'), 'class' => 'form-search')) }}

                <div class="row">

                    <div class="col-sm-3">
                        <div class="form-group">
                            @php
                                $name = 'company';
                                $translate = Translator::transSmart('app.Company', 'Company');
                            @endphp
                            <label for="{{$name}}" class="control-label">{{$translate}}</label>
                            {{ Form::select($name, $companies->pluck('name', 'name'), Request::get($name), array('id' => $name, 'title' => $translate, 'class' => 'form-control', 'title' => $name, 'placeholder' => '')) }}
                        </div>
                    </div>
                    <div class="col-sm-3">
                        <div class="form-group">
                            @php
                                $name = 'status';
                                $translate = Translator::transSmart('app.Status', 'Status');
                            @endphp
                            <label for="{{$name}}" class="control-label">{{$translate}}</label>
                            {{Form::select($name, Utility::constant('agos_status', true), Request::get($name), array('id' => $name, 'class' => 'form-control', 'title' => $translate, 'placeholder' => ''))}}
                        </div>
                    </div>
                    <div class="col-sm-3">
                        <div class="form-group">
                            @php
                                $name = 'from_date';
                                $translate = Translator::transSmart('app.From', 'From');
                            @endphp

                            <label for="{{$name}}" class="control-label">{{$translate}}</label>
                            <div class="input-group schedule">

                                {{Form::text($name, '' , array('id' => $name, 'class' => 'form-control datepicker', 'readonly' => 'readonly', 'title' => $translate, 'placeholder' => ''))}}
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            </div>
                        </div>
                    </div>
                    <div class="col-sm-3">
                        <div class="form-group">
                            @php
                                $name = 'to_date';
                                $translate = Translator::transSmart('app.To', 'To');
                            @endphp

                            <label for="{{$name}}" class="control-label">{{$translate}}</label>
                            <div class="input-group schedule">
                                {{Form::text($name, '' , array('id' => $name, 'class' => 'form-control datepicker', 'readonly' => 'readonly', 'title' => $translate, 'placeholder' => ''))}}
                                <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-sm-12 toolbar">
                        <div class="btn-toolbar pull-right">
                            <div class="btn-group">
                                {{
                                    Html::linkRouteWithIcon(
                                        null,
                                        Translator::transSmart('app.Search', 'Search'),
                                        'fa-search',
                                       array(),
                                       [
                                           'title' => Translator::transSmart('app.Search', 'Search'),
                                           'class' => 'btn btn-theme search-btn',
                                           'onclick' => "$(this).closest('form').submit();"
                                       ]
                                    )
                                }}
                            </div>
                        </div>
                    </div>
                </div>

                {{ Form::close() }}
            </div>
        </div>

        <div class="row" >
            <div class="col-sm-12">
                <hr />
            </div>
        </div><br>



        <div class="row" style="background-color:#FFFFFF">

            <div class="col-sm-12">


                <div class="table-responsive">
                    <table class="table table-condensed table-crowded">

                        <thead>
                        <tr>
                            <th>{{Translator::transSmart('app.#', '#')}}</th>
                            <th></th>
                            <th>{{Translator::transSmart('app.Company', 'Company')}}</th>
                            <th>{{Translator::transSmart('app.Products', 'Products')}}</th>
                            <th>{{Translator::transSmart('app.Total Price', 'Total Price')}}</th>
                            <th>{{Translator::transSmart('app.Status', 'Status')}}</th>
                            <th>{{Translator::transSmart('app.Created At', 'Created At')}}</th>
                            <th></th>
                        </tr>
                        </thead>
                        <tbody>
                        @if($orders->isEmpty())
                            <tr>
                                <td class="text-center empty" colspan="14">
                                    --- {{ Translator::transSmart('app.No Record.', 'No Record.') }} ---
                                </td>
                            </tr>
                        @endif
                        <?php $count = 0;?>
                        @foreach($orders as $order)
                            <tr>
                                <td>{{++$count}}</td>
                                <td></td>
                                <td>{{$order->name}}</td>
                                <td>
                                    @php
                                        $json = $order->data;
                                        $json = json_decode($json, true);
                                        $products = $json['order_info']['products'];

                                        $data = '';

                                            foreach ($products as $hitsIndex => $hitsValue) {
                                                $data .= $hitsValue['name']. ',  ';
                                            }
                                        $data = rtrim($data, ', ');
                                    @endphp

                                    {{$data}}
                                </td>
                                <td>
                                    @if(empty($order->price) || $order->price == 0)
                                        {{'Quotation'}}
                                    @else
                                        {{CLDR::showPrice($order->price, $order->currency, Config::get('money.precision'))}}
                                    @endif
                                </td>
                                <td>{{Utility::constant(sprintf('agos_status.%s.name', $order->status))}}</td>
                                <td>{{$order->created_at}}</td>
                                <td class="item-toolbox">
                                    {{
                                           Html::linkRouteWithIcon(
                                             'agos::edit',
                                            Translator::transSmart('app.Edit', 'Edit'),
                                            'fa-pencil',
                                            ['id' => $order->id],
                                            [
                                            'title' => Translator::transSmart('app.Edit', 'Edit'),
                                            'class' => 'btn btn-theme'
                                            ]
                                           )
                                    }}
                                </td>
                            </tr>
                        @endforeach
                        </tbody>

                    </table>
                </div>

                <div class="pagination-container">
                    @php
                        $query_search_param = Utility::parseQueryParams();
                    @endphp
                    {!! $orders->render() !!}
                </div>
            </div>
        </div>

    </div>
@endsection

Controller Code,

public function index(Request $request){
    try {
        $companies = (new Company())->showAllCompanyWithName(['name' => 'ASC'], false);
        $orders = (new Agos())->index();
    } catch (InvalidArgumentException $e) {
        return Utility::httpExceptionHandler(500, $e);
    } catch (Exception $e) {
        return Utility::httpExceptionHandler(500, $e);
    }

    $view = SmartView::render(null, compact($this->singular(), $this->plural(), 'companies', 'orders'));

    return $view;
}

Can someone helps me?

回答1:

Try to append the request in the results:

public function index(Request $request){
    try {
        $companies = (new Company())->showAllCompanyWithName(['name' => 'ASC'], false);
        $orders = (new Agos())->index();
        $queryArgs = Input::only(['status','company','from_date', 'to_date']);
        $orders->appends($queryArgs);
    } catch (InvalidArgumentException $e) {
        return Utility::httpExceptionHandler(500, $e);
    } catch (Exception $e) {
        return Utility::httpExceptionHandler(500, $e);
    }

    $view = SmartView::render(null, compact($this->singular(), $this->plural(), 'companies', 'orders'));

    return $view;
}

Then in your template:

<div class="pagination-container">
      {!! $orders->render(); !!}
</div>


回答2:

try to use in your Blade view

$orders->links()