MrZzippo p
MrZzippo p

Reputation: 24

Laravel pagination page 2 loses the data

When i fill my search and post it, it gets the data and returns it. In this query i use ->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);

My routes

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

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

The pagination on the page.

So that all works, but when i go to the second page, for some reason no result is there

when i search, it goes to this page http://172.16.0.51:8000/artikelen

and when i click on page 2 the link is http://172.16.0.51:8000/artikelen?page=2 But no data

Blade file

@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

The isset() returns false...

I am pretty sure when i click page 2 the data is getting lost but then would my question be how to i give that data through

Upvotes: 0

Views: 1399

Answers (1)

Mahdi Younesi
Mahdi Younesi

Reputation: 7509

You need to prevent default behavior of pagination links in Laravel, this solotion works for search urls like 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);
    }
});

Upvotes: 2

Related Questions