New: Learn Statamic on Laracasts!

We've retired the forum in favor of GitHub Discussions.

New conversations can be started on GitHub. Existing conversations will remain for a while longer.

Head over to GitHub →

Best way to render articles index page with Statamic and Laravel?

Connor Leech April 28, 2021 by Connor Leech

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 and then when user clicks "next page" it renders 12 more articles and the url is 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');


    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 = $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'] }}

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:


title: Articles
template: articles/show
layout: layout
  - categories
revisions: false
route: '/blog/{slug}'
date: true
sort_dir: asc
  past: public
  future: private


title: Pages
revisions: false
route: '{parent_uri}/{slug}'
  root: true
>>>>>>> Unanswered <<<<<<<
2 Replies