如何在此查询中使用paginate()?
$result = DB::select(DB::raw(" select a.user_id, a.user_email,
a.user_account_status, a.created_at, b.s_account_limit AS account_limit,
c.consumed_limit, ((b.s_account_limit*1024) - c.consumed_limit) AS
remaining_limit FROM upload_limits as b, users AS a JOIN user_upload_limits as
c WHERE (a.user_id=c.user_id) AND a.user_type='Simple'"));
答案 0 :(得分:2)
您可以使用->paginate()
方法,或者如果您想手动创建分页,则可以执行以下操作:
$perPage = $request->input("per_page", 10);
$page = $request->input("page", 1);
$skip = $page * $perPage;
if($take < 1) { $take = 1; }
if($skip < 0) { $skip = 0; }
$basicQuery = DB::select(DB::raw(" select a.user_id, a.user_email, a.user_account_status, a.created_at, b.s_account_limit AS account_limit, c.consumed_limit, ((b.s_account_limit*1024) - c.consumed_limit) AS remaining_limit FROM upload_limits as b, users AS a JOIN user_upload_limits as c WHERE (a.user_id=c.user_id) AND a.user_type='Simple'"));
$totalCount = $basicQuery->count();
$results = $basicQuery
->take($perPage)
->skip($skip)
->get();
$paginator = new \Illuminate\Pagination\LengthAwarePaginator($results, $totalCount, $take, $page);
return $paginator;
答案 1 :(得分:0)
您还可以添加一个自定义排序方法,您可以将DB :: raw的数组结果与params一起传递:
public function sort($array_of_objects, $sort_by=null, $order, $page)
{
$collection = collect($array_of_objects);
if ($sort_by)
{
if ($order=='desc') {
$sorted = $collection->sortBy(function($role) use ($sort_by)
{
return $role->{$sort_by};
})->reverse();
} else if ($order=='asc') {
$sorted = $collection->sortBy(function($role) use ($sort_by)
{
return $role->{$sort_by};
});
}
} else {
$sorted = $collection;
}
$num_per_page = 20;
if (!$page) {
$page = 1;
}
$offset = ( $page - 1) * $num_per_page;
$sorted = $sorted->splice($offset, $num_per_page);
return new Paginator($sorted, count($array_of_objects), $num_per_page, $page);
}