Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用原始集合的Laravel ajax分页_Php_Ajax_Laravel_Laravel 5_Pagination - Fatal编程技术网

Php 使用原始集合的Laravel ajax分页

Php 使用原始集合的Laravel ajax分页,php,ajax,laravel,laravel-5,pagination,Php,Ajax,Laravel,Laravel 5,Pagination,我有一个产品目录,可以通过几个不同的选择和复选框元素进行筛选。过滤操作通过ajax发布到SearchController,以便用户过滤时,结果立即显示。搜索词/变量未附加到URL中 我在分页方面遇到了一个问题,因为默认的Laravel分页会重新加载页面,因此会丢失用户已经过滤过的内容。为了快速解决这个问题,我将分页改为使用ajax 我的问题是ajax分页重新运行SearchController,以使集合抵消结果。然后再次重置搜索条件,因为没有使用分页ajax发布过滤器 当通过分页ajax调用时,

我有一个产品目录,可以通过几个不同的选择和复选框元素进行筛选。过滤操作通过ajax发布到
SearchController
,以便用户过滤时,结果立即显示。搜索词/变量未附加到URL中

我在分页方面遇到了一个问题,因为默认的Laravel分页会重新加载页面,因此会丢失用户已经过滤过的内容。为了快速解决这个问题,我将分页改为使用ajax

我的问题是ajax分页重新运行
SearchController
,以使集合抵消结果。然后再次重置搜索条件,因为没有使用分页ajax发布过滤器

当通过分页ajax调用时,我只想偏移原始集合。此结果集是否存在并可重用

public function filter(Request $request, Stock $Stock)
{
    // Enable query log
    DB::enableQueryLog();

    $Stock = $Stock->newQuery();

    if($request->has('model')) {
        $the_model = $request->model;
        $request->request->add([$the_model => 1]);
    }

    if ($request->has('car') && $request->input('car') == 'true') {
        $Stock->orWhere('car', 1);
    }

    if ($request->has('bike') && $request->input('bike') == 'true') {
        $Stock->orWhere('bike', 1);
    }

    if ($request->has('productTypes_chosen')) {

        $Stock->whereIn('productType', $request->productTypes_chosen);
    }

    if ($request->has('manufacturers_chosen')) {
        $Stock->whereIn('manufacturer', $request->manufacturers_chosen);
    }

    $Stock->where('show_website', 1);

    $stockItems = $Stock->get();
    $stockItems = $Stock->paginate(15);

    if($request->ajax()) {
        $view = view('partials.filtered-results')->with(compact('stockItems'));
        $view = $view->render();
        return $view;
    }
}
分页AJAX

$(function() {
    $('body').on('click', '.pagination a', function(e) {
        e.preventDefault();

        var url = $(this).attr('href');
        getStock(url);
        window.history.pushState("", "", url);
    });

    function getStock(url) {
        $.ajax({
            url : url
        }).done(function (data) {
            $('.machines').html(data);
            $('html, body').animate({
                scrollTop: $('.machines').offset().top - 150
            }, 'slow');
        }).fail(function () {
            alert('Stocklist could not be loaded.');
        });
    }
});

我相信要解决您的问题,您需要使用以下方法:

use Illuminate\Pagination\Paginator;

public function filter(Request $request, Stock $Stock)
{
    // Your filters here

    $page = $request->has('page') ?? 1;
    $limit = $request->has('limit') ?? 15;

    Paginator::currentPageResolver(function () use ($page) {
       return $page;
    });

    $stockItems = $Stock->paginate($limit);
}


您使用的laravel版本是什么?@KayoBruno laravel 6.0