my controller code
$posts = Categorie::where('parent_id',$id)->paginate(2);
model code :
public function post1(){
return $this->hasMany(post::class, 'category_id');
}
view page code :
@foreach ($posts as $cat)
@foreach ($cat->post1 as $post)
{{Debugbar::info($post->body)}}
<br>
@endforeach
@endforeach
and for pagination I used code :
{{ $posts->links() }}
all are working fine, data is fetched, I checked in the debugger. and pagination link also shown. I have 3 rows then fetched 2 rows on 1 page and other page shows another row. but problem is that it shows all data on page 1 when I clicked on pagination link page2 then shows a blank page. how my pagination work properly
no method "links" you can try this
{{ $posts->render() }}
You are currently paginating categories, not posts.
Define the reverse relationship of post1
(if you haven't already):
class Post extends Model {
public function category() {
return $this->belongsTo(Categorie::class, 'category_id');
}
}
Then query the Post
model and use whereHas()
:
$posts = Post::whereHas('category', function($query) use($id) {
$query->where('parent_id', $id);
})->paginate(2);
{{ $posts->links() }}
@foreach ($posts as $post)
{{ $post->body }}
@endforeach