Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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_Pagination_Eloquent - Fatal编程技术网

Php laravel中的分页问题

Php laravel中的分页问题,php,laravel,pagination,eloquent,Php,Laravel,Pagination,Eloquent,我的控制器代码 $posts = Categorie::where('parent_id',$id)->paginate(2); 型号代码: public function post1(){ return $this->hasMany(post::class, 'category_id'); } 查看页面代码: @foreach ($posts as $cat) @foreach ($cat->post1 as $post) {{Debugb

我的控制器代码

$posts = Categorie::where('parent_id',$id)->paginate(2);
型号代码:

public function post1(){
    return $this->hasMany(post::class, 'category_id');
}
查看页面代码:

@foreach ($posts as $cat)
    @foreach ($cat->post1 as $post)
        {{Debugbar::info($post->body)}}
        <br>
    @endforeach
@endforeach
一切正常,数据已提取,我已在调试器中检查。还显示了分页链接。我有3行,然后在1页上取了2行,另一页显示另一行。但问题是,当我点击分页链接page2时,它显示了第1页上的所有数据,然后显示了一个空白页。 我的分页如何正常工作

没有“链接”方法您可以尝试此方法

    {{ $posts->render() }}

您当前正在为类别而不是帖子分页

定义
post1
的反向关系(如果尚未定义):

然后查询
Post
模型并使用:


第二页的
$cat->post1
是否为空?是否要对类别或帖子进行分页?我要对帖子进行分页links()方法只是render()方法的别名。但有时不是work links()方法,因此请尝试render()方法。结果相同,renderI未发生任何更改获取此错误方法Illumb\Database\Query\Builder::category不存在。是否定义了反向关系?它叫
category
?我不明白你在说什么。请在问题中添加你的
Post
模型。
    {{ $posts->render() }}
class Post extends Model {
    public function category() {
        return $this->belongsTo(Categorie::class, 'category_id');
    }
}
$posts = Post::whereHas('category', function($query) use($id) {
    $query->where('parent_id', $id);
})->paginate(2);

{{ $posts->links() }}
@foreach ($posts as $post)
    {{ $post->body }} 
@endforeach