Skip to content

Commit 51b4a00

Browse files
committed
Merge branch 'release/2.1.0'
2 parents 5eaa0d2 + 819555d commit 51b4a00

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

2.0/requests/resources.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,34 @@ data that was validated for a resource create or update request.
815815
For relationship requests, you should use the `validatedForRelation`
816816
method to retrieve the validated value.
817817

818+
## Accessing Related Models
819+
820+
For requests that modify a relationship, it is possible to access the related
821+
model (for a to-one relationship) or models (for a to-many relationship). These
822+
are the models that are referenced in the `data` member of the request JSON.
823+
824+
:::warning
825+
As the request JSON is _untrusted_ input, you should only access the related
826+
model or models _after_ validation has successfully completed.
827+
:::
828+
829+
For to-one relationships, use the `toOne()` method. This method returns either
830+
the related model or `null`, depending on what is in the request JSON. For
831+
example:
832+
833+
```php
834+
/** @var \App\Models\User|null $author */
835+
$author = $request->toOne();
836+
```
837+
838+
For to-many relationships, use the `toMany()` method. This method returns a
839+
collection of the related models referenced in the request JSON. For example:
840+
841+
```php
842+
/** @var \Illuminate\Support\Collection $tags */
843+
$tags = $request->toMany();
844+
```
845+
818846
## Deleting Resources
819847

820848
It is possible to add validation rules for deleting resources. This is useful

2.0/routing/controllers.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,9 @@ public function updatingAuthor(
627627
): void
628628
{
629629
// do something...
630+
// to access the author use:
631+
/** @var \App\Models\User|null $author */
632+
$author = $request->toOne();
630633
}
631634

632635
public function updatingTags(
@@ -636,6 +639,9 @@ public function updatingTags(
636639
): void
637640
{
638641
// do something...
642+
// to access the tags, use:
643+
/** @var \Illuminate\Support\Collection $tags */
644+
$tags = $request->toMany();
639645
}
640646
```
641647

@@ -672,6 +678,9 @@ public function updatedAuthor(
672678
): void
673679
{
674680
// do something...
681+
// to access the author use:
682+
/** @var \App\Models\User|null $author */
683+
$author = $request->toOne();
675684
}
676685

677686
public function updatedTags(
@@ -682,6 +691,9 @@ public function updatedTags(
682691
): void
683692
{
684693
// do something...
694+
// to access the tags, use:
695+
/** @var \Illuminate\Support\Collection $tags */
696+
$tags = $request->toMany();
685697
}
686698
```
687699

@@ -719,6 +731,9 @@ public function attachingTags(
719731
): void
720732
{
721733
// do something...
734+
// to access the tags, use:
735+
/** @var \Illuminate\Support\Collection $tags */
736+
$tags = $request->toMany();
722737
}
723738
```
724739

@@ -751,6 +766,9 @@ public function attachedTags(
751766
): void
752767
{
753768
// do something...
769+
// to access the tags, use:
770+
/** @var \Illuminate\Support\Collection $tags */
771+
$tags = $request->toMany();
754772
}
755773
```
756774

@@ -788,6 +806,9 @@ public function detachingTags(
788806
): void
789807
{
790808
// do something...
809+
// to access the tags, use:
810+
/** @var \Illuminate\Support\Collection $tags */
811+
$tags = $request->toMany();
791812
}
792813
```
793814

@@ -820,6 +841,9 @@ public function detachedTags(
820841
): void
821842
{
822843
// do something...
844+
// to access the tags, use:
845+
/** @var \Illuminate\Support\Collection $tags */
846+
$tags = $request->toMany();
823847
}
824848
```
825849

2.0/schemas/attributes.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,15 @@ use LaravelJsonApi\Eloquent\Fields\Boolean;
346346
Boolean::make('active')
347347
```
348348

349+
:::tip
350+
When validating a boolean field, we recommend using our `boolean` rule instead
351+
of Laravel's `boolean` rule. This is because the Laravel rule is loosely-typed,
352+
i.e. it will accept string values as booleans. However, our `boolean` rule
353+
ensures the JSON value is a _real_ boolean. See the
354+
[Validating Booleans](../requests/resources.md#validating-booleans)
355+
section for more information.
356+
:::
357+
349358
### DateTime Field
350359

351360
The `DateTime` field may be used to represent an attribute that is a string
@@ -449,6 +458,25 @@ use LaravelJsonApi\Eloquent\Fields\Number;
449458
Number::make('failures', 'failure_count')
450459
```
451460

461+
By default our `Number` field only accepts integers or floats. This is because
462+
JSON has a number type, which decodes to a PHP integer or float. However, there
463+
is a _gotcha_ with this. Laravel's `integer` and `numeric` validation rules are
464+
loosely-typed: i.e. they will accept a numeric string.
465+
466+
We therefore recommend using our `number` or `integer` validation rules, as
467+
described in the
468+
[Validating Numbers](../requests/resources.md#validating-numbers)
469+
section.
470+
471+
If however you do want to accept numeric strings, call the `acceptStrings()`
472+
method on the `Number` field:
473+
474+
```php
475+
use LaravelJsonApi\Eloquent\Fields\Number;
476+
477+
Number::make('failures')->acceptStrings();
478+
```
479+
452480
### String Field
453481

454482
The `Str` (string) field may be used to represent an attribute that is

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy