I have an laravel
-application where I want to add pagination to my "blog-posts"-section. I can make it work when I return all blog_posts, but since they are categorized, the pagination does not work anymore.
Here is my controller
public function index() {
$blog_posts = Blog::where("publishes_on", "<=", Carbon::now())
->orderBy('publishes_on', 'desc')
->where('blog_status_id', '=', '2')
->with('blog_category')
->paginate(4);
$blog_categories = BlogCategory::get();
return view('blog.index', compact('blog_categories', 'blog_posts'));
}
and the blade view
@foreach($blog_categories as $category)
@foreach($category->blog_posts as $blog)
<div>
<a href="/blog/{{ $blog->slug }}">{{ $blog->title }}</a>
</div>
@endforeach
@endforeach
Before I added the categories, my blade view looked like this
@foreach($blog_posts as $blog)
<div style="border-top: 2px solid red; padding: 4px;">
<a href="/blog/{{ $blog->slug }}">{{ $blog->title }}</a>
</div>
@endforeach
<div>
{{ $blog_posts->links() }}
</div>
but now, I don't know how to add the pagination?
can someone help me out
You can create your custom pagination: (https://laravel.com/api/5.8/Illuminate/Pagination/LengthAwarePaginator.html)