Laravel:分页只返回第一块

时间:2017-06-22 20:52:46

标签: php laravel pagination laravel-5.4

我通过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;
}

1 个答案:

答案 0 :(得分:1)

因为查询参数不会附加到后续的分页请求中。

将路线内的逻辑更新为:

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

OR

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