laravel 方法 paginate 不存在。转换为对象后在laravel中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48275148/
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
Method paginate does not exist. in laravel after conversion to object
提问by
I'm getting this error for pagination
我在分页时遇到此错误
Method paginate does not exist.
方法 paginate 不存在。
$allTodaysJob = \DB::select(\DB::raw('select * from `new_job` WHERE DATE(created_at) = DATE(CURRENT_TIMESTAMP())'));
$collection = collect($allTodaysJob)->paginate(10);
return view('common_status',['data'=>$collection]);
Please help me to solve this issue.
请帮我解决这个问题。
回答by Salar Bahador
Paginate works with Eloquent model. Make a model and then If you use model you can do something like this with eloquent:
Paginate 与 Eloquent 模型一起使用。创建一个模型,然后如果你使用模型,你可以用 eloquent 做这样的事情:
$allTodaysJob = ModelName::where('created_at', DATE(CURRENT_TIMESTAMP())->get()->paginate(10);
Or if you want to order it by latest:
或者,如果您想最晚订购:
$allTodaysJob = ModelName::where('created_at', DATE(CURRENT_TIMESTAMP())->latest()->paginate(10);
But if it you want to use raw query you can make a custom pagination method in the current class. first, you make an array, then you pass that array to the paginator method like this code below:
但是如果你想使用原始查询,你可以在当前类中创建一个自定义分页方法。首先,您创建一个数组,然后将该数组传递给分页器方法,如下面的代码:
this is the pagination method:
这是分页方法:
protected function paginate($items,$perPage,$request)
{
$page = Input::get('page', 1); // Get the current page or default to 1
$offset = ($page * $perPage) - $perPage;
return new LengthAwarePaginator(
array_slice($items, $offset, $perPage, true),
count($items), $perPage, $page,
['path' => $request->url(), 'query' => $request->query()]
);
}
Then you can call the paginate method after you select data from the database, I would recomend to do it with raw method instead of select:
然后您可以在从数据库中选择数据后调用 paginate 方法,我建议使用原始方法而不是选择:
$allTodaysJob = \DB::raw('select * from `new_job` WHERE DATE(created_at) = DATE(CURRENT_TIMESTAMP())')->get();
$allTodaysJob = $this->paginate($allTodaysJob,10,$request);
Note that you should pass the Request $request to the index method and then use it in the pagination.Because from that request to specific page through pagination link laravel pagination know which items to select to show in your view.
请注意,您应该将 Request $request 传递给 index 方法,然后在分页中使用它。因为通过分页链接从该请求到特定页面,laravel 分页知道要选择在您的视图中显示哪些项目。
Hope it would help!
希望它会有所帮助!
回答by Sohel0415
Paginate
works with eloquent model
or query builder
, it does not work with raw sql query
. Make a model of table new_job
as NewJob
.
Paginate
与eloquent model
或 一起使用query builder
,它不适用于raw sql query
. 做表的模型new_job
作为NewJob
。
$collection = NewJob::where('created_at',date("Y-m-d H:i:s"))->paginate(10);
回答by Adam Kozlowski
In my opinion it should go that way:
在我看来,它应该是这样的:
$collection = collect($allTodaysJob)->get()->toArray()->paginate(10);
回答by Abdulla Nilam
try these
试试这些
$allTodaysJob = \DB::select('select * from `new_job` WHERE DATE(created_at) = DATE(CURRENT_TIMESTAMP())')->paginate(10);
return view('common_status',['data'=>$allTodaysJob]);
Or (I'm using currently)
或者(我目前正在使用)
$allTodaysJob = DB::table('new_job')
->where('created_at', DATE(CURRENT_TIMESTAMP()))
->paginate(10);
return view('common_status',['data'=>$allTodaysJob]);