简体   繁体   中英

Laravel Pagination the data not change with the render() in the blade

I have this problem, when i manually create a paginator in laravel for show 100 products, in the view the page displays the data and it is fine, but if i put a limit , example i want 10 element per page, he show the ten elements in the firs page, when i click in next the second page show me the same ten elements, the data don't changes , why?

Controller:

  public function show()
    {


        $client = new Client([
            // Base URI is used with relative requests
            'base_uri' => 'http://www.mocky.io/v2/59bec4d926000046015261a7',
            // You can set any number of default request options.
            'timeout' => 2.0,
        ]);

        $response = $client->request('GET', '');
        $code = $response->getStatusCode()
        $products = json_decode($response->getBody()->getContents());

        }

     $products =  new Paginator($products, 10 ,
       Paginator::resolveCurrentPage(),
       ['path' => Paginator::resolveCurrentPath()]);


        return view('products/list', compact('products'));

    }

View

@extends('layout.master')

@section('content')
<h2> Products</h2>
<ul>

    @if($products)
    @foreach($products as $product)
       <li> {{ $product->name}} - {{ $product->value}}</li>
        @endforeach
        @endif

</ul>
{{$products->render()}}

@endsection

Example of Result with array of ten element , 3 per page // this is a example with invented information.

array {0,1,2,3,4,5,6,7,8,9}

Page 1 

 0 - 0 
 1 - 1
 2 - 2

Page 2 // the data dont change , why ?

 0 - 0 
 1 - 1
 2 - 2 

No magic, paginators will call your controller function for every page. The request will have the pagination information in it. It is your job to actually select and slice the page. The paginator simply presents it... which is a big part of the work...

   // DB::select returns an array, thus we have to build the paginator ourselves...
    $comm = DB::select('select bla bla bla from comments where this and that... 
                        order by approved ASC');

    // this basically gets the request's page variable... or defaults to 1
    $page = Paginator::resolveCurrentPage('page') ?: 1;

    // Assume 15 items per page... so start index to slice our array
    $startIndex = ($page - 1) * 15;

    // Length aware paginator needs a total count of items... to paginate properly
    $total = count($comm);

    // Eliminate the non relevant items...
    $results = array_slice($comm, $startIndex, 15);

    $comments =  new LengthAwarePaginator($results, $total, 15, $page, [
        'path' => Paginator::resolveCurrentPath(),
        'pageName' => 'page',
    ]);
    return view('backend/comments', compact('comments'));

You need to add your page name (the name of the request param denoting the page number) like so:

$products =  new Paginator($products, 10, null,
       ['path' => Paginator::resolveCurrentPath(),
        'pageName' => 'page']);

I had the same problem, where the data was not changing. I solved it by passing a page number to my guzzle call. Whenever a link is clicked, a request is sent therefore, you can get the page number from the request object. Pass the page number to the guzzle call so that the page number changes. Hope It helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM