我怎样才能对这个原始查询进行分页?

How can i paginate this raw query?

我正在 Laravel 中从事一个项目,并使用 DB facade 来 运行 sql 的原始查询。在我的例子中,我使用的是 DB::select,问题是分页方法无法使用它并显示此错误

调用数组的成员函数 paginate()

如何对这个原始查询进行分页?这是我的代码:

$que= DB::select("SELECT * FROM tbl_ourpeople INNER JOIN tbl_ourpeople_category ON
tbl_ourpeople.category = tbl_ourpeople_category.categoryId WHERE tbl_ourpeople.id>1");
return view('view',compact('que'));

试试这个:

$query = DB::table('tbl_ourpeople')
    ->join('tbl_ourpeople_category', 'tbl_ourpeople.category', '=', 'tbl_ourpeople_category.categoryId')
    ->where('tbl_ourpeople.id', '>', 1)
    ->paginate(15);

对于纯原始查询,您可以使用这种方式。

$perPage = $request->input("per_page", 10);
$page = $request->input("page", 1);
$skip = $page * $perPage;
if($take < 1) { $take = 1; }
if($skip < 0) { $skip = 0; }

$que = DB::select(DB::raw("SELECT * FROM tbl_ourpeople INNER JOIN tbl_ourpeople_category ON
tbl_ourpeople.category = tbl_ourpeople_category.categoryId WHERE tbl_ourpeople.id>1"));

$totalCount = $que->count();
$results = $que
    ->take($perPage)
    ->skip($skip)
    ->get();

$paginator = new \Illuminate\Pagination\LengthAwarePaginator($results, $totalCount, $take, $page);

return $paginator;