laravel paginate动态分页

1.router

Route::get('product', function(){
    $products = AppProduct::paginate(10);
    return view('product.index', compact('products'));
});

Route::get('ajax/product', function(){
    $products = AppProduct::paginate(10);
    return view('product.indexAjax', compact('products'));
});

2.indexAjax.blade.php

<div class="row">
        <div class="col-md-6 col-md-offset-3">
            <h2>Products list</h2>
            <ul>
                @foreach($products as $product)
                    <li><h3>{{ $product->name }}</h3><span class='pull-right'>{{ $product->id }}</span></li>
                @endforeach
            </ul>
            {!! $products->render() !!}
        </div>
</div>

3.js

    <script type="text/javascript">
    $(document).on('click', '.pagination a', function(e){
        e.preventDefault();

        page = $(this).attr('href').split('page=')[1];

        getProducts(page);
    });

    function getProducts(page){
        $.ajax({
            url : 'ajax/product?page=' + page,
        }).done(function(date){
            $('.container').html(date);
            location.hash = page;
        });
    }
    </script>
原文地址:https://www.cnblogs.com/fenle/p/4855969.html