【问题标题】:Laravel: Pagination only returning first chunkLaravel:分页只返回第一个块
【发布时间】:2017-06-22 20:52:45
【问题描述】:

我在 Laravel 应用程序中通过 AJAX 请求提取分页数据。这适用于第一个块。但是,对于以下内容,它只传递一列(id)。

有什么问题?

相关代码:

Route::get('contacts', function (Request $request) {
    return buildQuery($request, User::class)->simplePaginate(50);
});

function buildQuery(Request $request, $model) {
    $query = DB::table('users')->select('id as ID');
    $properties = $request->all();

    $searchString = "";
    if (in_array('q', array_keys($properties)) && $properties['q']) {
        $searchString = $properties['q'];
        $query->orWhere('id', 'like', '%'.$searchString.'%');
    }

    foreach ($properties as $property => $value) {
        if ($property == "id" || $property == "q")
            continue;

        if (in_array($property, array_keys($model::$labels))) {
            $query->addSelect($property.' as '.$model::$labels[$property]);

            if ($value == "desc" || $value == "asc")
                $query->orderBy($property, $value);

            if ($searchString)
                $query->orWhere($property, 'like', '%'.$searchString.'%');
        }
    }

    return $query;
}

【问题讨论】:

    标签: php laravel pagination laravel-5.4


    【解决方案1】:

    这是因为查询参数没有附加到后续的分页请求中。

    更新您在 Route 中的逻辑:

    Route::get('contacts', function (Request $request) {
        return buildQuery($request, User::class)->paginate(50)->appends($request->all());
    });
    

    Route::get('contacts', function (Request $request) {
            return buildQuery($request, User::class)->simplePaginate(50)->appends($request->all());
        });
    

    【讨论】:

    • 导致:BadMethodCallException:方法追加不存在。
    • 好吧,我的错。它适用于 LengthAwarePaginator。如果您需要它来进行简单分页,请在从中构建链接时使用查询返回的结果的附加信息。
    • 同样的错误。调用对象 append 是一个 AbstractPaginator
    • 好的,我打错了。它应该是“附加”一个's'。它应该适用于 paginate 和 simplePaginate 方法。
    • 你当之无愧的打勾^^
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    • 2020-02-02
    • 1970-01-01
    • 2019-03-22
    相关资源
    最近更新 更多