方法分页不存在.在拉拉维尔转换为对象后



我收到分页错误

方法分页不存在。

$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]); 

请帮我解决这个问题。

Paginate 使用 Eloquent 模型。创建一个模型,然后如果你使用模型,你可以用雄辩来做这样的事情:

$allTodaysJob = ModelName::where('created_at', DATE(CURRENT_TIMESTAMP())->get()->paginate(10); 

或者,如果您想按最新订购:

$allTodaysJob = ModelName::where('created_at', DATE(CURRENT_TIMESTAMP())->latest()->paginate(10); 

但是,如果您想使用原始查询,则可以在当前类中创建自定义分页方法。 首先,创建一个数组,然后将该数组传递给分页器方法,如下代码:

这是分页方法:

 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()]
        );
    }

然后,您可以在从数据库中选择数据后调用分页方法,我建议使用原始方法而不是选择:

$allTodaysJob = DB::raw('select * from `new_job` WHERE DATE(created_at) = DATE(CURRENT_TIMESTAMP())')->get();
$allTodaysJob = $this->paginate($allTodaysJob,10,$request);

请注意,应将请求$request传递给索引方法,然后在分页中使用它。因为从该请求到特定页面通过分页链接 laravel 分页知道要选择要在您的视图中显示哪些项目。

希望对您有所帮助!

在我看来,它应该是这样的:

 $collection = collect($allTodaysJob)->get()->toArray()->paginate(10);

Paginate适用于eloquent modelquery builder,它不适用于raw sql query。将表new_job的模型制作为NewJob

$collection = NewJob::where('created_at',date("Y-m-d H:i:s"))->paginate(10);

试试这些

$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]); 

最新更新