0
votes

Hi I made a Laravel Project which I can view some records with sorting. For examle I have a deliveries page with URL

http://localhost/dboard/public/deliverytracker

In that form there are From Date and To Date to sort out the data before the query. When I clicked submit the data is rendered and the pagination links is there. However when click, for example page 2 of the pagination which is

http://localhost/dboard/public/deliverytracker?page=2

in the link it won't rendered the page 2 of the data. It just reloaded the page where I need to select again the from date and to date then clicking submit again. Did I missed something?

Here's my route

/* Delivery Tracker */
Route::get('deliverytracker', 'DeliveryTrackerController@deliveryIndex');
Route::post('deliverytracker', 'DeliveryTrackerController@getDeliveries');

My function when I submit the form

public function getDeliveries()
{
    $rules = array(
        'fromdate'  => 'required',
        'todate'    => 'required',
    );

    $validator = Validator::make(Input::all(), $rules);

    // Check if all fields is filled
    if ($validator->fails()) 
    {
        return Redirect::to('deliverytracker')->withErrors($validator);
    }
    else
    {
        $from   = Input::get('fromdate');
        $to     = Input::get('todate');

        $deliveries = new DeliveryTracking();
        $result = $deliveries->getDeliveries($from, $to);

        // Get the current page from the url if it's not set default to 1
        $page = Input::get('page', 1); 

        // Number of items per page
        $perPage = 5;

        // Start displaying items from this number;
        $offSet = ($page * $perPage) - $perPage; // Start displaying items from this number

        // Get only the items you need using array_slice (only get 10 items since that's what you need)
        $itemsForCurrentPage = array_slice($result, $offSet, $perPage, true);

        // Return the paginator with only 10 items but with the count of all items and set the it on the correct page
        $result =  new LengthAwarePaginator($itemsForCurrentPage, count($result), $perPage, $page);
        $result->setPath('deliverytracker');

        return view('deliverytracker.index')->with(array('result' => $result));

    }

}

My view

<div class="panel-body">
                @if(isset($result))
                    <table class="table table-striped table-bordered">
                    <thead>
                        <tr>
                            <td>Delivery Status</td>
                            <td>Ref. Number</td>
                            <td>PO #</td>
                            <td>Count</td>
                            <td>Delivery QTY</td>
                            <td>Date Delivered</td>
                            <td>Filename</td>
                            <td>Invoice #</td>
                            <td>Date Invoice</td>

                        </tr>
                    </thead>
                    <tbody>
                    @foreach($result as $key => $value)
                        @if($value->stat == "Completed")
                            <tr>
                                <td class="bg-success">{{ $value->stat  }}</td>
                                <td class="bg-success">{{ $value->oc }}</td>
                                <td class="bg-success">{{ $value->pon }}</td>
                                <td class="bg-success">{{ $value->cnt }}</td>
                                <td class="bg-success">{{ $value->dq }}</td>
                                <td class="bg-success">{{ $value->dd }}</td>
                                <td class="bg-success">{{ $value->fn }}</td>
                                <td class="bg-success">{{ $value->inum }}</td>
                                <td class="bg-success">{{ $value->di }}</td>
                            </tr>
                        @elseif($value->stat == "Active")
                            <tr>
                                <td class="bg-danger">{{ $value->stat  }}</td>
                                <td class="bg-danger">{{ $value->oc }}</td>
                                <td class="bg-danger">{{ $value->pon }}</td>
                                <td class="bg-danger">{{ $value->cnt }}</td>
                                <td class="bg-danger">{{ $value->dq }}</td>
                                <td class="bg-danger">{{ $value->dd }}</td>
                                <td class="bg-danger">{{ $value->fn }}</td>
                                <td class="bg-danger">{{ $value->inum }}</td>
                                <td class="bg-danger">{{ $value->di }}</td>
                            </tr>   
                        @endif

                    @endforeach
                    </tbody>
                     </table>
                        {!! $result->render() !!}
                @else 
                    No records found.    

                @endif
 </div>
2
where do you set your fromdate and todate; because you made them required and they are not added to the URL for the other pages, you get redirected: see Appending To Pagination LinksLuceos

2 Answers

0
votes

In your post you mention that the second page is called using only ?page=N, seeing your route is defined with both GET and POST you will hit the route definition for GET when clicking the next page link.

The pages buttons are simply links and not POST forms. So instead of hitting the getDeliveries() you are hitting deliveryIndex().

Also you might consider appending the fromdate and todate to your pagination links; see Appending To Pagination Links

0
votes

Form fields(fromdate, todate) are not automatically attached to the pagination. You need to add query string values to paginator using ->append() method.

// Return the paginator with only 10 items but with the count of all items and set the it on the correct page
$result =  new LengthAwarePaginator($itemsForCurrentPage, count($result), $perPage, $page);
$result->setPath('deliverytracker');
$result->appends(['fromdate' => $from, 'todate' => $todate]);

Reference: http://laravel.com/api/5.0/Illuminate/Contracts/Pagination/Paginator.html#method_appends