我的分页错误
方法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]);
请帮我解决这个问题。
答案 0 :(得分:3)
Paginate与Eloquent模型合作。制作一个模型然后如果你使用模型你可以用eloquent做这样的事情:
$allTodaysJob = ModelName::where('created_at', DATE(CURRENT_TIMESTAMP())->get()->paginate(10);
或者如果您想通过最新订购:
$allTodaysJob = ModelName::where('created_at', DATE(CURRENT_TIMESTAMP())->latest()->paginate(10);
但是如果您想使用原始查询,则可以在当前类中创建自定义分页方法。首先,你创建一个数组,然后将该数组传递给paginator方法,如下面的代码所示:
这是分页方法:
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()]
);
}
然后你可以在从数据库中选择数据后调用paginate方法,我建议用raw方法而不是select来实现:
$allTodaysJob = \DB::raw('select * from `new_job` WHERE DATE(created_at) = DATE(CURRENT_TIMESTAMP())')->get();
$allTodaysJob = $this->paginate($allTodaysJob,10,$request);
请注意,您应该将Request $请求传递给index方法,然后在分页中使用它。因为从该请求到特定页面,通过分页链接laravel分页知道要在您的视图中选择要显示的项目。
希望它会有所帮助!
答案 1 :(得分:1)
在我看来应该这样:
$collection = collect($allTodaysJob)->get()->toArray()->paginate(10);
答案 2 :(得分:1)
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);
答案 3 :(得分:0)
试试这些
$allTodaysJob = \DB::select('select * from `new_job` WHERE DATE(created_at) = DATE(CURRENT_TIMESTAMP())')->paginate(10);
return view('common_status',['data'=>$allTodaysJob]);
或(我现在正在使用)
$allTodaysJob = DB::table('new_job')
->where('created_at', DATE(CURRENT_TIMESTAMP()))
->paginate(10);
return view('common_status',['data'=>$allTodaysJob]);