Skip to content

Commit c052016

Browse files
[5.x] Document new findOrFail methods on repositories (#1305)
* Tweak wording on "Entry Repository" page * Document `findOrFail` on other repositories
1 parent 1a298d1 commit c052016

9 files changed

+46
-9
lines changed

content/collections/repositories/asset-container-repository.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use Statamic\Facades\AssetContainer;
2626
| `all()` | Get all AssetContainers |
2727
| `find($id)` | Get AssetContainer by `id` |
2828
| `findByHandle($handle)` | Get AssetContainer by `handle` |
29+
| `findOrFail($id)` | Get AssetContainer by `id`. Throws an `AssetContainerNotFoundException` when the asset container cannot be found. |
2930
| `queryAssets()` | Query Builder for the AssetContainer's [Assets](#assets) |
3031
| `make()` | Makes a new AssetContainer instance |
3132

@@ -45,6 +46,12 @@ $videos->queryAssets()
4546
->get();
4647
```
4748

49+
When an asset container can't be found, the `AssetContainer::find()` method will return `null`. If you'd prefer an exception be thrown, you may use the `findOrFail` method:
50+
51+
```php
52+
AssetContainer::findOrFail('videos');
53+
```
54+
4855
## Creating
4956

5057
Start by making an instance of an asset container with the `make` method. You can pass the handle into it.
@@ -71,4 +78,3 @@ Finally, save it.
7178
```php
7279
$container->save();
7380
```
74-

content/collections/repositories/asset-repository.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ use Statamic\Facades\Asset;
2020
| Methods | Description |
2121
| ------- | ----------- |
2222
| `all()` | Get all Assets |
23-
| `find()` | Get Asset by `filename` |
24-
| `findByPath()` | Get Asset by `path` |
25-
| `findByUrl()` | Get Asset by `url` |
23+
| `find($filename)` | Get Asset by `filename` |
24+
| `findByPath($path)` | Get Asset by `path` |
25+
| `findByUrl($url)` | Get Asset by `url` |
26+
| `findOrFail($filename)` | Get Asset by `filename`. Throws an `AssetNotFoundException` when the asset cannot be found. |
2627
| `query()` | Query Builder |
27-
| `whereContainer()` | Find Assets by [AssetContainer](#asset-container) |
28-
| `whereFolder()` | Find Assets in a filesystem folder |
28+
| `whereContainer($container)` | Find Assets by [AssetContainer](#asset-container) |
29+
| `whereFolder($folder)` | Find Assets in a filesystem folder |
2930
| `make()` | Makes a new `Asset` instance |
3031

3132
:::tip

content/collections/repositories/collection-repository.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ use Statamic\Facades\Collection;
2323
| `all()` | Get all Collections |
2424
| `find($id)` | Get Collection by `id` |
2525
| `findByHandle($handle)` | Get Collection by `handle` |
26-
| `findByMount($mount)` | Get Asset by mounted entry `id` |
26+
| `findByMount($mount)` | Get Collection by mounted entry `id` |
27+
| `findOrFail($id)` | Get Collection by `id`. Throws a `CollectionNotFoundException` when the collection cannot be found. |
2728
| `handleExists($handle)` | Check to see if `Collection` exists |
2829
| `handles()` | Get all `Collection` handles |
2930
| `queryEntries()` | Query Builder for [Entries](/repositories/entry-repository) |
@@ -44,6 +45,12 @@ $blog = Collection::find('blog');
4445
$blog->queryEntries()->get();
4546
```
4647

48+
When a collection can't be found, the `Collection::find()` method will return `null`. If you'd prefer an exception be thrown, you may use the `findOrFail` method:
49+
50+
```php
51+
Collection::findOrFail('blog');
52+
```
53+
4754
## Creating
4855

4956
Start by making an instance of a collection with the `make` method. You can pass the handle into it.

content/collections/repositories/entry-repository.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use Statamic\Facades\Entry;
2222
| `all()` | Get all Entries |
2323
| `find($id)` | Get Entry by `id` |
2424
| `findByUri($uri, $site)` | Get Entry by `uri`, optionally in a site |
25-
| `findOrFail($id)` | Get Entry by `id`. Throws an `EntryNotFoundException` when entry can not be found. |
25+
| `findOrFail($id)` | Get Entry by `id`. Throws an `EntryNotFoundException` when the entry cannot be found. |
2626
| `query()` | Query Builder |
2727
| `whereCollection($handle)` | Get all Entries in a `Collection` |
2828
| `whereInCollection([$handles])` | Get all Entries in an array of `Collections` |
@@ -41,7 +41,7 @@ Entry::query()->where('id', 123)->first();
4141
Entry::find(123);
4242
```
4343

44-
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:
44+
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:
4545

4646
```php
4747
Entry::findOrFail(123);

content/collections/repositories/form-repository.md

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use Statamic\Facades\Form;
2020
| ------- | ----------- |
2121
| `all()` | Get all Forms |
2222
| `find($handle)` | Get Form by `handle` |
23+
| `findOrFail($handle)` | Get Form by `handle`. Throws a `FormNotFoundException` when the form cannot be found. |
2324
| `make()` | Makes a new `Form` instance |
2425

2526
:::tip
@@ -36,6 +37,12 @@ The `handle` is the name of the form's YAML file.
3637
Form::find('postbox');
3738
```
3839

40+
When a form can't be found, the `Form::find()` method will return `null`. If you'd prefer an exception be thrown, you may use the `findOrFail` method:
41+
42+
```php
43+
Form::findOrFail('postbox');
44+
```
45+
3946
#### Get all forms from your site
4047

4148
```php

content/collections/repositories/global-repository.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use Statamic\Facades\GlobalSet;
2020
| `all()` | Get all GlobalSets |
2121
| `find($id)` | Get GlobalSets by `id` |
2222
| `findByHandle($handle)` | Get GlobalSets by `handle` |
23+
| `findOrFail($id)` | Get GlobalSets by `id`. Throws a `GlobalSetNotFoundException` when the global set cannot be found. |
2324
| `make()` | Makes a new `GlobalSet` instance |
2425

2526
## Querying

content/collections/repositories/taxonomy-repository.md

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use Statamic\Facades\Taxonomy;
2424
| `find($id)` | Get Taxonomy by `id` |
2525
| `findByHandle($handle)` | Get Taxonomy by `handle` |
2626
| `findByUri($mount)` | Get Taxonomy by `uri` |
27+
| `findOrFail($id)` | Get Taxonomy by `id`. Throws a `TaxonomyNotFoundException` when the taxonomy cannot be found. |
2728
| `handleExists($handle)` | Check to see if `Taxonomy` exists |
2829
| `handles()` | Get all `Taxonomy` handles |
2930
| `queryTerms()` | Query Builder for [Terms](/content-queries/term-repository) |
@@ -43,6 +44,12 @@ $tags = Taxonomy::find('tags');
4344
$tags->queryTerms()->get();
4445
```
4546

47+
When a taxonomy can't be found, the `Taxonomy::find()` method will return `null`. If you'd prefer an exception be thrown, you may use the `findOrFail` method:
48+
49+
```php
50+
Taxonomy::findOrFail('tags');
51+
```
52+
4653
## Creating
4754

4855
Start by making an instance of a taxonomy with the `make` method. You can pass the handle into it.

content/collections/repositories/term-repository.md

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ use Statamic\Facades\Term;
2323
| `all()` | Get all Terms |
2424
| `find($id)` | Get Term by `id` |
2525
| `findByUri($uri)` | Get Term by `uri` |
26+
| `findOrFail($id)` | Get Term by `id`. Throws a `TermNotFoundException` when the term cannot be found. |
2627
| `query()` | Query Builder |
2728
| `make()` | Makes a new `Term` instance |
2829

content/collections/repositories/user-repository.md

+7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use Statamic\Facades\User;
2424
| `find($id)` | Get User by `id` |
2525
| `findByEmail($email)` | Get User by `email` |
2626
| `findByOAuthID($provider, $id)` | Get User by an ID from an OAuth provider |
27+
| `findOrFail($id)` | Get User by `id`. Throws a `UserNotFoundException` when the user cannot be found. |
2728
| `query()` | Query Builder |
2829
| `make()` | Makes a new `User` instance |
2930

@@ -40,6 +41,12 @@ User::query()
4041
User::find('abc123');
4142
```
4243

44+
When a user can't be found, the `User::find()` method will return `null`. If you'd prefer an exception be thrown, you may use the `findOrFail` method:
45+
46+
```php
47+
User::findOrFail('abc123');
48+
```
49+
4350
#### Get a user by email
4451

4552
```php

0 commit comments

Comments
 (0)