Laravel分页第2页丢失了数据

时间:2018-03-28 14:14:36

标签: php laravel pagination request

当我填写搜索并发布时,它会获取数据并将其返回。 在此查询中,我使用 - > paginate(50);

$products = Product7Days::where('omschrijving', 'LIKE', '%'.$request->omschrijving.'%')
                                    ->where('artikelnummer', 'LIKE', '%'.$request->artikelnummer.'%')
                                    ->whereIn('relatienummer', $leverancierenArray)
                                    ->whereIn('omzetgroep', $omzetgroepenArray)
                                    ->whereIn('btwcode', $BTWArray)
                                    ->whereBetween('prijs', [$prijsVan, $prijsTot])
                                    ->where('totaalaantal', '>=', $aantalVan)
                                    ->where('totaalaantal', '<=', $aantalTot)
                                    ->paginate(50);

我的路线

Route::get('/artikelen', 'ProductController@index')->name('artikelen');

Route::post('/artikelen', 'ProductController@search')->name('artikelen.search');

The pagination on the page.

所以一切正常,但是当我进入第二页时,出于某种原因没有结果

当我搜索时,它会转到此页面 http://172.16.0.51:8000/artikelen

当我点击第2页时,链接就是 http://172.16.0.51:8000/artikelen?page=2 但没有数据

刀片文件

@if (isset($products))
                    <table class="table table-striped table-responsive-xl">
                        <thead class="thead-dark">
                            <tr>
                                <th>Artikelnummer</th>
                                <th>Omschrijving</th>
                                <th>Prijs</th>
                                <th>Totaal <br> verkocht</th>
                                <th>Totaal <br> omzet</th>
                                @foreach ($filialen as $filiaal)
                                @if ($filiaal !== 6)
                                <th>aantal. <br>F{{$filiaal}}</th>
                                <th>voorr. <br>F{{$filiaal}}</th>
                                @else
                                <th>voorr. <br>F{{$filiaal}}</th>
                                @endif
                                @endforeach
                            </tr>
                        </thead>
                        <tbody>
                        @foreach ($products as $product)
                            <tr>
                                <th>{{ $product->artikelnummer}}</th>
                                <th>{{ $product->omschrijving}}</th>
                                <th>{{ $product->prijs}}</th>
                                <th>{{ $product->totaalaantal}}</th>
                                <th class="text-right">{{ round($product->totaalaantal * $product->prijs, 1) }}</th>
                                @foreach ($filialen as $filiaal)
                                @if ($filiaal !== 6)
                                <th class="text-right">{{ $product["aantalF".$filiaal] }}</th>
                                <th class="text-right">{{ $product["voorraadF".$filiaal] }}</th>
                                @else
                                <th>{{ $product->voorraadF6 }}</th>
                                @endif
                                @endforeach
                            </tr>
                        @endforeach
                        </tbody>
                    </table>
                    {{ $products->links() }}
                    @endif

isset()返回false ...

我很确定当我点击第2页时数据会丢失,但我的问题是如何通过

提供数据

1 个答案:

答案 0 :(得分:0)

您需要阻止Laravel中分页链接的默认行为,此solotion适用于http://172.16.0.51:8000/search?property1=x&property2=y

等搜索网址
$(document).on('click', '.pagination li a', function (e) {
    e.preventDefault();
    if ($(this).attr('href')) {
        var queryString = '';
        var allQueries = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
        if(allQueries[0].split('=').length >1){
            for (var i = 0; i < allQueries.length; i++) {
                var hash = allQueries[i].split('=');
                if (hash[0] !== 'page') {
                    queryString += '&' + hash[0] + '=' + hash[1];
                }
            }
        }
        window.location.replace($(this).attr('href') + queryString);
    }
});