Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 23, 2023
1 parent fda485b commit 7b319ca
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]),
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
]);
Expand Down
2 changes: 1 addition & 1 deletion src/Fields/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
40 changes: 37 additions & 3 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
});
}
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/BelongsToManyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 7b319ca

Please sign in to comment.