Laravel 使用 paginate() 和 select() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21173657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:53:07  来源:igfitidea点击:

Laravel using paginate() with select() method

laravelpaginate

提问by SomeRandomDude

Is it possible to use paginate() with the select() method? Using the code below I get the error:

是否可以将 paginate() 与 select() 方法一起使用?使用下面的代码我得到错误:

Call to a member function paginate() on a non-object

在非对象上调用成员函数 paginate()

$query = 'SELECT * FROM items';
$results = DB::select($query)->paginate(10);

I know the following code works with paginate() with just the table() method:

我知道以下代码仅使用 table() 方法与 paginate() 一起使用:

$results = DB::table('plans_shared')->paginate(10);

It seems much easier for me to use the select() since I have many conditional AND/WHERE clauses

对我来说使用 select() 似乎容易多了,因为我有很多条件 AND/WHERE 子句

回答by Vladislav Rastrusny

select() method returns array so paginate() cannot be called on it. You can only try to use \Paginator::make(DB::select($query))if you like

select() 方法返回数组,因此不能对其调用 paginate()。你只能尝试使用\Paginator::make(DB::select($query))如果你喜欢

回答by Arbitur

If you have an array and want to paginate it use Paginate::make()Paginate Laravel 4.2

如果您有一个数组并想对其进行分页,请使用Paginate::make()Paginate Laravel 4.2

Ex:

前任:

$pages = Paginate::make(DB::select($query), 10);