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 |
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 methodEntry::find(123);
Get an entry by its URI
Entry::query()->where('uri', 'blog/my-first-post')->first(); // Or with the shorthand methodEntry::findByUri('/blog/my-first-post');
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
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();
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. ->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