From 7b319caee1e9226d1c844056de06d4a029ace78b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Thu, 23 Nov 2023 13:49:20 +0100 Subject: [PATCH] wip --- src/Fields/BelongsToMany.php | 2 +- src/Fields/Media.php | 2 +- src/Fields/Meta.php | 2 +- src/Fields/Relation.php | 40 +++++++++++++++++-- .../Controllers/BelongsToManyController.php | 2 +- src/Http/Controllers/RelationController.php | 2 +- 6 files changed, 42 insertions(+), 8 deletions(-) diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index fc035ebd..2dee9f2e 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -293,7 +293,7 @@ public function toCreate(Request $request, Model $model): array $pivot = $relation->newPivot(); - $pivot->setRelation('related', $relation->getRelated()); + $pivot->setRelation('related', $relation->make()); return array_merge($this->toSubResource($request, $model), [ 'title' => __('Attach :model', ['model' => $this->getRelatedName()]), diff --git a/src/Fields/Media.php b/src/Fields/Media.php index 89a28147..0d2ec1b6 100644 --- a/src/Fields/Media.php +++ b/src/Fields/Media.php @@ -197,7 +197,7 @@ public function toInput(Request $request, Model $model): array 'x-model.debounce.300ms' => $filter->getKey(), 'x-bind:readonly' => 'processing', ]) - ->toInput($request, $this->getRelation($model)->getRelated()); + ->toInput($request, $this->getRelation($model)->make()); }) ->all(), ]); diff --git a/src/Fields/Meta.php b/src/Fields/Meta.php index 22537794..c7310f93 100644 --- a/src/Fields/Meta.php +++ b/src/Fields/Meta.php @@ -21,7 +21,7 @@ class Meta extends MorphOne public function __construct(string $label, string $modelAttribute = null, Closure|string $relation = null) { $relation ??= function (Model $model): EloquentRelation { - $related = $model->metaData()->getRelated(); + $related = $model->metaData()->make(); return $model->metaData() ->one() diff --git a/src/Fields/Relation.php b/src/Fields/Relation.php index e8062c51..26b09512 100644 --- a/src/Fields/Relation.php +++ b/src/Fields/Relation.php @@ -77,6 +77,16 @@ abstract class Relation extends Field implements Form */ protected bool $asSubResource = false; + /** + * The relations to eager load on every query. + */ + protected array $with = []; + + /** + * The relations to eager load on every query. + */ + protected array $withCount = []; + /** * The query scopes. */ @@ -385,13 +395,37 @@ public function getPerPageOptions(): array return [5, 10, 15, 25]; } + /** + * The relations to be eagerload. + */ + public function with(array $with): static + { + $this->with = $with; + + return $this; + } + + /** + * The relation counts to be eagerload. + */ + public function withCount(array $withCount): static + { + $this->withCount = $withCount; + + return $this; + } + /** * Paginate the given query. */ public function paginate(Request $request, Model $model): LengthAwarePaginator { return tap($this->getRelation($model), function (EloquentRelation $relation) use ($request): void { - $this->resolveFilters($request)->apply($request, $relation->getQuery())->latest(); + $this->resolveFilters($request) + ->apply($request, $relation->getQuery()) + ->with($this->with) + ->withCount($this->withCount) + ->latest(); })->paginate($request->input('per_page', 5))->withQueryString(); } @@ -492,7 +526,7 @@ public function registerRouteConstraints(Request $request, Router $router): void { $router->bind($this->getRouteKeyName(), function (string $id) use ($request): Model { return $id === 'create' - ? $this->getRelation($request->route()->parentOfParameter($this->getRouteKeyName()))->getRelated() + ? $this->getRelation($request->route()->parentOfParameter($this->getRouteKeyName()))->make() : $this->resolveRouteBinding($request, $id); }); } @@ -568,7 +602,7 @@ public function toCreate(Request $request, Model $model): array { return array_merge($this->toSubResource($request, $model), [ 'title' => __('Create :model', ['model' => $this->getRelatedName()]), - 'model' => $related = $this->getRelation($model)->getRelated(), + 'model' => $related = $this->getRelation($model)->make(), 'action' => $this->modelUrl($model), 'method' => 'POST', 'fields' => $this->resolveFields($request) diff --git a/src/Http/Controllers/BelongsToManyController.php b/src/Http/Controllers/BelongsToManyController.php index 9db4259d..297114aa 100644 --- a/src/Http/Controllers/BelongsToManyController.php +++ b/src/Http/Controllers/BelongsToManyController.php @@ -23,7 +23,7 @@ public function store(Request $request, Model $model): RedirectResponse $relation->getForeignPivotKeyName() => $model->getKey(), ]); - $pivot->setRelation('related', $relation->getRelated()); + $pivot->setRelation('related', $relation->make()); $pivot->incrementing = true; diff --git a/src/Http/Controllers/RelationController.php b/src/Http/Controllers/RelationController.php index 7f0485d8..ca1790fc 100644 --- a/src/Http/Controllers/RelationController.php +++ b/src/Http/Controllers/RelationController.php @@ -48,7 +48,7 @@ public function store(Request $request, Model $model): RedirectResponse { $field = $request->route('field'); - $related = $field->getRelation($model)->getRelated(); + $related = $field->getRelation($model)->make(); $field->handleFormRequest($request, $related);