Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Laravel集合实现分页?_Php_Laravel_Eloquent_Laravel Collection_Laravel Pagination - Fatal编程技术网

Php 如何使用Laravel集合实现分页?

Php 如何使用Laravel集合实现分页?,php,laravel,eloquent,laravel-collection,laravel-pagination,Php,Laravel,Eloquent,Laravel Collection,Laravel Pagination,据我所知,Laravel分页不适用于Laravel集合(雄辩)。例如: $articles = Article::with('category')->where('status', 'submitted')->get(); 我正试图用上面截取的代码进行分页,但where('status','submitted')不起作用 控制器 $articles = Article::paginate(10); 刀片 @foreach($articles->with('category'

据我所知,Laravel分页不适用于Laravel集合(雄辩)。例如:

$articles = Article::with('category')->where('status', 'submitted')->get();
我正试图用上面截取的代码进行分页,但
where('status','submitted')
不起作用

控制器

$articles = Article::paginate(10);
刀片

@foreach($articles->with('category')->where('status', 'submitted') as $article)
    { ... }
    {{ $articles->links() }} 

我犯了一个错误

在where子句之后对查询进行分页:

$articles = Article::with('category')->where('status', 'submitted')->paginate(10);
在您的刀片中:

{{ $articles->links() }}

@foreach($articles as $article)
    { ... }
@endforeach

在where子句之后为查询分页:

$articles = Article::with('category')->where('status', 'submitted')->paginate(10);
在您的刀片中:

{{ $articles->links() }}

@foreach($articles as $article)
    { ... }
@endforeach

再加上@Remul的答案,如果您仍然希望对集合进行分页,可以使用该方法。例如:

$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]);

$chunk = $collection->forPage(2, 3); 
// first argument is the page number 
// second one is the number of items to show per page

$chunk->all();

再加上@Remul的答案,如果您仍然希望对集合进行分页,可以使用该方法。例如:

$collection = collect([1, 2, 3, 4, 5, 6, 7, 8, 9]);

$chunk = $collection->forPage(2, 3); 
// first argument is the page number 
// second one is the number of items to show per page

$chunk->all();

在分页之前添加with/where子句。基本上,取代码的第一位,用get换paginate。得到的错误是什么?在pagination子句之前添加with/where。基本上,取第一位代码,用get换paginate。您得到的错误是什么?非常感谢。它起作用了,我知道在这种情况下该怎么办。非常感谢。它奏效了,我明白在这种情况下该怎么办。