|
4 | 4 |
|
5 | 5 | use Binaryk\LaravelRestify\Contracts\RestifySearchable; |
6 | 6 | use Binaryk\LaravelRestify\Controllers\RestResponse; |
| 7 | +use Binaryk\LaravelRestify\Eager\Related; |
7 | 8 | use Binaryk\LaravelRestify\Exceptions\InstanceOfException; |
8 | 9 | use Binaryk\LaravelRestify\Fields\BelongsToMany; |
9 | 10 | use Binaryk\LaravelRestify\Fields\EagerField; |
@@ -511,23 +512,24 @@ public function resolveRelationships($request): array |
511 | 512 | { |
512 | 513 | $withs = collect(); |
513 | 514 |
|
514 | | - /** * To avoid circular relationships and deep stack calls, we will do not load eager fields. */ |
515 | | - if (! $this->isEagerState()) { |
516 | | - $this->collectFields($request) |
517 | | - ->forEager($request, $this) |
518 | | - ->filter(fn (EagerField $field) => $field->isShownOnShow($request, $this)) |
519 | | - ->each(fn (EagerField $field) => $withs->put($field->attribute, $field->resolve($this)->value)); |
520 | | - } |
| 515 | + static::collectRelated() |
| 516 | + ->authorized($request) |
| 517 | + ->inRequest($request) |
| 518 | + ->mapIntoRelated($request) |
| 519 | + ->each(function (Related $related) use ($request, $withs) { |
| 520 | + $relation = $related->getRelation(); |
521 | 521 |
|
522 | | - collect(str_getcsv($request->input('related'))) |
523 | | - ->filter(fn ($relation) => in_array($relation, static::getRelated())) |
524 | | - ->each(function ($relation) use ($request, $withs) { |
525 | 522 | if (Str::contains($relation, '.')) { |
526 | 523 | $this->resource->loadMissing($relation); |
527 | 524 |
|
528 | 525 | return $withs->put($key = Str::before($relation, '.'), Arr::get($this->resource->relationsToArray(), $key)); |
529 | 526 | } |
530 | 527 |
|
| 528 | + /** * To avoid circular relationships and deep stack calls, we will do not load eager fields. */ |
| 529 | + if ($related->isEager() && $this->isEagerState() === false) { |
| 530 | + return $withs->put($relation, $related->resolveField($this)->value); |
| 531 | + } |
| 532 | + |
531 | 533 | $paginator = $this->resource->relationLoaded($relation) |
532 | 534 | ? $this->resource->{$relation} |
533 | 535 | : $this->resource->{$relation}(); |
@@ -861,6 +863,11 @@ public function allowToDestroy(RestifyRequest $request) |
861 | 863 | return $this; |
862 | 864 | } |
863 | 865 |
|
| 866 | + /** |
| 867 | + * @param $request |
| 868 | + * @return $this |
| 869 | + * @throws \Illuminate\Auth\Access\AuthorizationException |
| 870 | + */ |
864 | 871 | public function allowToShow($request): self |
865 | 872 | { |
866 | 873 | $this->authorizeToShow($request); |
|
0 commit comments