Radical Design Course by Jack McDade

From the creator of Statamic

Learn how to make your websites standout and be remembered.

Taking your approach on designing things actually makes it fun, more natural, and overall easier.

— Dominik, Developer

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 the entry cannot 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 may 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', 'news')
->where('author', $author->id())
->get();

Get all entries using a specific Blueprint

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

Get all published entries

Entry::query()
->whereStatus('published')
->get();
Hot Tip!

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

Get all entries with taxonomy terms

When you want to query all entries with a specific term, you should use the whereTaxonomy method:

Entry::query()
->where('collection', 'news')
->whereTaxonomy('categories::laravel')
->get();

In the above example, categories is the taxonomy's handle and laravel is the term's slug.

When you want to query all entries with multiple terms, you should instead use the whereTaxonomyIn method:

Entry::query()
->where('collection', 'news')
->whereTaxonomyIn(['categories::laravel', 'categories::statamic'])
->get();
Warning!

This only works when the taxonomy is linked in your collection's config. If it's not linked in the collection config, you should use the whereJsonContains method instead:

Entry::query()
->where('collection', 'news')
->whereJsonContains('categories_field', ['laravel', 'statamic'])
->get();

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

Setting an entry's parent

When you're creating entries in structured collections, you may find yourself needing to programatically set an entry's parent.

Parent & children pages are stored in structures, which live in "trees", rather than in the entry data. This means that in order to set an entry's parent, we will need to append it to the structure.

// Create your entry as normal...
$entry = Entry::make()
->collection('pages')
->slug('about')
->data([
// ...
]);
 
 
// Before saving the entry, define an "afterSave" callback to append the
// new entry to the tree, under the parent.
$entry->afterSave(function ($entry) {
$parent = Entry::find('the_parent_entry');
 
$entry->collection()
->structure()
->in($entry->locale())
->appendTo($parent->id(), $entry->id())
->save();
});
 
// And finally, save the entry.
$entry->save();

Localization

Localizing an entry

After an entry has been created, you can call the makeLocalization method to localize the entry into another site:

$entry->makeLocalization('fr');

Getting an entry's localizations

You can use various methods to get an entry's localizations:

// Check if the entry has been localized in a specific site...
$entry->existsIn('fr');
 
// Get the localized entry in a specific site...
$entry->in('fr');
 
// Get all "descendants" of the entry (those entries localized from the current entry)...
$entry->descendants();
 
// Get all "ancestors" of the entry (those entries the current entry is localized from)...
$entry->ancestors();
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 →