Laravel 5 Pagination如何在另一个页面中渲染结果

时间:2015-06-16 09:05:17

标签: php laravel pagination laravel-5

您好我做了一个Laravel项目,我可以通过排序查看一些记录。例如,我有一个带URL的交付页面

http://localhost/dboard/public/deliverytracker

在该表单中,有“从日期”和“到”日期以在查询之前对数据进行排序。 当我点击提交时,数据会被渲染,并且分页链接就在那里。但是,当点击时,例如

的分页的第2页
http://localhost/dboard/public/deliverytracker?page=2

在链接中它不会呈现数据的第2页。它只是重新加载页面,我需要再次选择从日期和日期,然后再次单击提交。我错过了什么吗?

这是我的路线

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

我提交表格时的功能

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));

    }

}

我的观点

<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 个答案:

答案 0 :(得分:0)

在您的帖子中,您提到第二页仅使用?page=N进行调用,看到您的路线定义为GETPOST,您将点击{{1}的路线定义点击下一页链接

页面按钮只是链接而不是POST表单。因此,不是点击GET,而是点击getDeliveries()

您也可以考虑将fromdate和todate附加到您的分页链接;见Appending To Pagination Links

答案 1 :(得分:0)

表单字段(fromdate,todate)不会自动附加到分页。您需要使用 - &gt; append()方法将查询字符串值添加到paginator。

// 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]);

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