Get Entries Of This Week with Statamic and Laravel Blade

March 26th, 2023

First, we need to query our Statamic Entries in our collection : \Statamic\Facades\Entry:query()->where('collection', 'blog')

and then add to query: date start and date end

Our query should look like this:

1$this_week_posts = \Statamic\Facades\Entry::query()->where('collection', 'blog')
2 ->where('date', '>=', $startOfWeek)->where('date', '<', $endOfWeek)->orderBy('date')->get();

How to get date that represents start of the week and end of the week?

Easy with Carbon methods startOfWeek and endOfWeek. First we need to create today date with Carbon\CarbonImmutable:now(). It has to be immutable, so it doesn't change (mutate) during querying. You can read more info about Carbon Immutable here.

1@php
2 $today = Carbon\CarbonImmutable::now();
3 $startOfWeek = $today->startOfWeek();
4 $endOfWeek = $today->endOfWeek();
5 
6 $this_week_posts = \Statamic\Facades\Entry::query()->where('collection', 'blog')
7 ->where('date', '>=', $startOfWeek)->where('date', '<', $endOfWeek)->orderBy('date')->get();
8@endphp
9 
10// show this week's posts

Related Posts