你不能按照你想要的方式去做。因为 $infoList->paginate(..) 方法是在 Eloquent 模型/构建器中构建的,并且您拥有 Illuminate\Database\Query\Builder 实例。
从DB查看外观。
对此有多种解决方案。您可以将$infoList 数组放在一个集合中并使用此slice 方法。
$infoList = DB::connection('mysql')
->select(DB::raw('CALL dbname.GetAllUserInfo()'));
$list = collect($infoList); // or new Collection($list);
// paginate/slice the array into the set you want
// first parameter: offset, second parameter the amount/limit
$pageList = $list->slice($request->get('offset', 0), 30);
另一种解决方案是创建一个模型并且不使用存储过程(但我猜这不是您需要的解决方案)。
class User extends Illuminate\Database\Eloquent\Model
{
// model implementation
}
$infoList = User::paginate(30); // parameter: limit/size
另一种解决方案是也使用存储过程并在存储过程调用中进行分页:
// stored procedure
DELIMITER //
CREATE PROCEDURE GetAllUserInfo (
IN _limit smallint unsigned,
IN _offset smallint unsigned
)
BEGIN
SELECT Name, HeadOfState FROM Country
WHERE Continent = con
LIMIT _offset, _limit;
END //
DELIMITER ;
// execute the stored procedure
$infoList = DB::connection('mysql')
->select(DB::raw('CALL dbname.GetAllUserInfo('. $request->get('offset', 0) .', 30)'));
注意:我写了一个存储过程已经有一段时间了,所以我写它的形状不太好。我把它写在我头顶的atm上。
我希望这些解决方案之一对您有用。