0

Laravel Call to a member function paginate() on array

renata 发布于2年前 阅读数 3911

laravel 里面使用DB::select($sql)->paginate();不能使用

举报
回复
0

下面是一个例子。

$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;
2年前 回复
0

不建议这样使用, 如果有很复杂的SQL也可以用Laravel的query封装起来,比如像下面的这样。

        $users = \DB::table('users')->select(\DB::raw('users.id,if(cargo_owners.status is  null, vehicle_owners.status, cargo_owners.status) as status, if(cargo_owners.name is  null, vehicle_owners.name, cargo_owners.name) as name,users.`created_at`,cargo_owners.type sub_type, users.type'))
            ->leftJoin('cargo_owners', 'cargo_owners.id'  , 'users.id')
            ->leftJoin('vehicle_owners', 'vehicle_owners.id' , 'users.id');
 $users->paginate();
2年前 回复

相关搜索关键词