Hey there, I've migrated my posts from a database to Statamic and things are going well so far. One thing I'm a little confused about though is how to render a list of blog posts with pagination. For example I'd like to render 12 articles on mysite.com/blog and then when user clicks "next page" it renders 12 more articles and the url is mysite.com/blog?page=2 or something like that..
I've come up with a solution using Laravel and I think I'm making it too complicated but this is what I have. It doesn't do pagination but instead fetches all of the articles in the Statamic DB, puts them in a Laravel collection, sorts it and passes it to a Blade view. Please save me lol
Route defined in routes/web.php:
Route::get('/blog', '[email protected]')->name('posts');
app/Http/Controllers/PostController.php
public function index(Request $request)
{
if (Cache::has('posts')) {
$posts = Cache::get('posts');
return view('blog.index', compact('posts'));
}
$entries = Entry::query()->where('collection', 'articles')->get();
$posts = collect();
foreach($entries as $entry) {
$posts->push($entry->toAugmentedArray());
}
$posts = $posts->sortByDesc('date_published');
Cache::put('posts', $posts);
return view('blog.index', compact('posts'));
Then in that blade file I loop through the entries:
@foreach($posts as $entry)
<p class="text-xl font-semibold text-gray-900">
{{ $entry['title'] }}
</p>
@endforeach
I'm totally fine using Antlers and Statamic routing and everything. My question is what's the "Statamic way" to render a list of articles on a page within a Laravel application?
If it's helpful these are the Articles and Pages collections I've defined in Statamic:
Articles:
title: Articles
template: articles/show
layout: layout
taxonomies:
- categories
revisions: false
route: '/blog/{slug}'
date: true
sort_dir: asc
date_behavior:
past: public
future: private
Pages:
title: Pages
revisions: false
route: '{parent_uri}/{slug}'
structure:
root: true