Radical Design Course by Jack McDade

From the creator of Statamic

Learn how to make your websites standout and be remembered.

Bought Jack McDade's course on design. Going through it now...and it is SO well done!

— Justin Jackson, Transistor.fm

Entry Repository

To work with the Entry Repository, use the following Facade:

use Statamic\Facades\Entry;

Methods

Methods Description
all() Get all Entries
find($id) Get Entry by id
findByUri($uri, $site) Get Entry by uri, optionally in a site
findOrFail($id) Get Entry by id. Throws an EntryNotFoundException when entry can not be found.
query() Query Builder
whereCollection($handle) Get all Entries in a Collection
whereInCollection([$handles]) Get all Entries in an array of Collections
make() Makes a new entry instance

Querying

Examples

Get a single entry by its id

Entry::query()->where('id', 123)->first();
 
// Or with the shorthand method
Entry::find(123);

When an entry can't be found, the Entry::find() method will return null. If you'd prefer an exception be thrown, you can use the findOrFail method:

Entry::findOrFail(123);

Get an entry by its URI

Entry::query()->where('uri', 'blog/my-first-post')->first();
 
// Or with the shorthand method
Entry::findByUri('/blog/my-first-post');
Hot Tip!

What is the difference between URI and URL? URL includes the site root (e.g. /fr/ in a multisite), if there is one, while URI is site agnostic and will not. As you may have surmised, when you only have a single site — they are identical.

Get all entries in a collection

Entry::query()
->where('collection', 'blog')
->get();

Get an entry from a collection by its slug

Entry::query()
->where('collection', 'blog')
->where('slug', 'my-first-post')
->first();

Get an entry by its slug in a multi-site install

Entry::query()
->where('collection', 'team')
->where('slug', 'director')
->where('site', 'albuquerque')
->get();

Get all Pre-Y2K news

Entry::query()
->where('collection', 'news')
->where('date', '<', '2000')
->get();

Get all of today's news

use Illuminate\Support\Carbon;
 
Entry::query()
->where('collection', 'news')
->where('date', Carbon::parse('today'))
->get();

Get the last 12 months of news

use Illuminate\Support\Carbon;
 
Entry::query()
->where('collection', 'news')
->where('date', '>=', Carbon::parse('now')->subYears(1))
->get();

Find all entries authored by Jack

$author = User::findByEmail('[email protected]');
 
Entry::query()
->where('collection', $handle)
->where('author', $author->id())
->get();

Get all entries using a specific Blueprint

Entry::query()
->where('blueprint', 'editorial')
->get();

Get all published and scheduled entries

Entry::query()
->whereIn('status', ['published', 'scheduled'])
->get();
Hot Tip!

What is the difference between querying against published and status? Read more on date behavior and published status!

Creating

Start by making an instance of an entry with the make method.
You need at least a slug and the collection before you can save an entry.

$entry = Entry::make()->collection('blog')->slug('my-entry');

You may call additional methods on the entry to customize it further.

$entry
->date($carbon) // or string of Y-m-d or Y-m-d-Hi
->published(true) // or false for a draft
->locale('default') // the site handle. defaults to the default site.
->blueprint('article') // set entry blueprint
->data(['foo' => 'bar']) // an array of data (front-matter)
->origin($origin); // another entry instance

Finally, save it. It'll return a boolean for whether it succeeded.

$entry->save(); // true or false
HR: Section
Learn More!

There is more to learn more in these related articles:

Fieldtypes

shipment-container

Repositories

HR: Section
Docs feedback

Submit improvements, related content, or suggestions through Github.

Betterify this page →