Skip to content

Commit f5f9412

Browse files
committed
wip
1 parent 3ef12c6 commit f5f9412

File tree

1 file changed

+79
-1
lines changed

1 file changed

+79
-1
lines changed

fields.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,47 @@ $field->asSubResource();
622622

623623
Subresources appear on the parent resource models's show route.
624624

625+
##### Fields
626+
627+
When using a field as subresource, you may define the related model's fields:
628+
629+
```php
630+
$field->withFields(static function (Request $request): array {
631+
return [
632+
Textarea::make('Body'),
633+
Boolean::make('Approved'),
634+
];
635+
});
636+
```
637+
638+
##### Filters
639+
640+
When using a field as subresource, you may define the filters for its `index` view:
641+
642+
```php
643+
use App\Root\Filters\CategoryFilter;
644+
645+
$field->withFilters(static function (Request $request): array {
646+
return [
647+
new CategoryFilter,
648+
];
649+
});
650+
```
651+
652+
##### Actions
653+
654+
When using a field as subresource, you may define the filters for its `index` view:
655+
656+
```php
657+
use App\Root\Actions\SendOrderNotification;
658+
659+
$field->withActions(static function (Request $request): array {
660+
return [
661+
new SendOrderNotification,
662+
];
663+
});
664+
```
665+
625666
### BelongsTo
626667

627668
The `BelongsTo` field is typically a handler for an existing `Illuminate\Database\Eloquent\Relations\BelongsTo` relation:
@@ -640,7 +681,7 @@ The `BelongsToMany` field is typically a handler for a `Illuminate\Database\Eloq
640681
$field = BelongsToMany::make(__('Teams'), 'teams');
641682
```
642683

643-
When using as subresource, you may define editable pivot fields:
684+
When using as subresource, you may define editable pivot fields. Use the `withPivotFields` instead of the `withFields`:
644685

645686
> Only existing models can be attached, creating relatable models from the subresource is not supported.
646687
@@ -655,14 +696,51 @@ $field->withPivotFields(static function (Request $request): array {
655696
});
656697
```
657698

699+
Sometimes you may want to attach the same model multiple times (maybe with different pivot values). To do so, call the `allowDuplicateRelations` method on the field:
700+
701+
```php
702+
$field->allowDuplicateRelations();
703+
```
704+
705+
When duplications are allowed, the relatable model query includes the already attached models as well, otherwise they are excluded from the query.
706+
658707
### HasMany
659708

660709
The `HasMany` field is typically a handler for a `Illuminate\Database\Eloquent\Relations\HasMany` relation:
661710

711+
```php
712+
$field = HasMany::make(__('Comments'), 'comments');
713+
```
714+
715+
When using as subresource, you may define the related model's fields:
716+
717+
```php
718+
$field->withFields(static function (Request $request): array {
719+
return [
720+
Textarea::make('Body'),
721+
Boolean::make('Approved'),
722+
];
723+
});
724+
```
725+
662726
### HasOne
663727

664728
The `HasOne` field is typically a handler for a `Illuminate\Database\Eloquent\Relations\HasOne` relation:
665729

730+
```php
731+
$field = HasMany::make(__('Comments'), 'comments');
732+
```
733+
734+
When using as subresource, you may define the related model's fields:
735+
736+
```php
737+
$field->withFields(static function (Request $request): array {
738+
return [
739+
Textarea::make('Body'),
740+
Boolean::make('Approved'),
741+
];
742+
});
743+
666744
### MorphMany
667745

668746
The `MorphMany` field is typically a handler for a `Illuminate\Database\Eloquent\Relations\MorphMany` relation:

0 commit comments

Comments
 (0)