Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 19, 2023
1 parent 3062ff3 commit 532f8d0
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 19 deletions.
2 changes: 1 addition & 1 deletion resources/views/resources/relation.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<turbo-frame id="relation-{{ $attribute }}">
<turbo-frame id="relation-{{ $attribute }}" data-turbo-action="advance">
@include('root::table.table')
</turbo-frame>
2 changes: 1 addition & 1 deletion resources/views/resources/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</div>
</div>

@if($model->exists)
@if($model->exists && isset($relations))
<div class="l-row__column">
@foreach($relations as $relation)
<turbo-frame id="relation-{{ $relation['attribute'] }}" src="{{ $relation['url'] }}">
Expand Down
4 changes: 2 additions & 2 deletions resources/views/table/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ class="form-check__control"
<td>
<div class="data-table__actions">
@can('view', $row['model'])
<a href="{{ $row['url'] }}" class="btn btn--light btn--sm btn--icon" aria-label="{{ __('View') }}">
<a href="{{ $row['url'] }}" class="btn btn--light btn--sm btn--icon" aria-label="{{ __('View') }}" data-turbo-frame="_top">
<x-root::icon name="eye" class="btn__icon" />
</a>
@endcan
@can('update', $row['model'])
<a href="{{ $row['url'] }}/edit" class="btn btn--light btn--sm btn--icon" aria-label="{{ __('Edit') }}">
<a href="{{ $row['url'] }}/edit" class="btn btn--light btn--sm btn--icon" aria-label="{{ __('Edit') }}" data-turbo-frame="_top">
<x-root::icon name="edit" class="btn__icon" />
</a>
@endcan
Expand Down
14 changes: 14 additions & 0 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,4 +224,18 @@ public function toValidate(Request $request, Model $model): array
$this->resolveFields($request)->mapToValidate($request, $model)
);
}

/**
* Get the edit representation of the relation.
*/
public function toShow(Request $request, Model $model, Model $related): array
{
$relation = $this->getRelation($model);

$pivot = $related->getRelation($relation->getPivotAccessor());

$pivot->setRelation('related', $related);

return parent::toShow($request, $model, $pivot);
}
}
52 changes: 41 additions & 11 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,14 @@ public function mapRelated(Request $request, Model $model, Model $related): arra
];
}

/**
* Resolve the resource model for a bound value.
*/
public function resolveRouteBinding(Request $request, Model $model, string $id): Model
{
return $this->getRelation($model)->findOrFail($id);
}

/**
* Register the routes using the given router.
*/
Expand All @@ -414,6 +422,7 @@ public function routes(Router $router): void
$router->get('/', [RelationController::class, 'index']);
$router->get('/create', [RelationController::class, 'create']);
$router->get('/{resourceRelation}', [RelationController::class, 'show']);
$router->get('/{resourceRelation}/edit', [RelationController::class, 'edit']);
}
}

Expand Down Expand Up @@ -488,33 +497,54 @@ public function toIndex(Request $request, Model $model): array
public function toCreate(Request $request, Model $model): array
{
return array_merge($this->toSubResource($request, $model), [
'title' => __('Create :model', ['model' => $this->getRelationName()]),
'title' => __('Create :model', ['model' => $this->getRelatedName()]),
'model' => $related = $this->getRelation($model)->getRelated(),
'action' => $this->getUri(),
'action' => $this->replaceRoutePlaceholders($request->route()), $related->getRouteKey(),
'method' => 'POST',
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $related)
->visible('create')
->visible('relation.create')
->mapToInputs($request, $related),
]);
}

/**
* Get the edit representation of the resource.
* Get the edit representation of the relation.
*/
public function toShow(Request $request, Model $model, Model $related): array
{
return array_merge($this->toSubResource($request, $model), [
'title' => sprintf('%s #%s', $this->getRelatedName(), $related->getKey()),
'model' => $related,
'action' => sprintf('%s/%s', $this->replaceRoutePlaceholders($request->route()), $related->getRouteKey()),
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $related)
->visible('relation.show')
->mapToDisplay($request, $related),
'actions' => $this->resolveActions($request)
->authorized($request, $related)
->visible('relation.show')
->mapToForms($request, $related),
]);
}

/**
* Get the edit representation of the relation.
*/
public function toEdit(Request $request, Model $model): array
public function toEdit(Request $request, Model $model, Model $related): array
{
return array_merge($this->toSubResource($request, $model), [
'title' => __('Edit :model', ['model' => sprintf('%s #%s', $this->getRelationName(), $model->getKey())]),
'model' => $model,
'url' => '',
'title' => __('Edit :model', ['model' => sprintf('%s #%s', $this->getRelatedName(), $related->getKey())]),
'model' => $related,
'url' => sprintf('%s/%s', $this->replaceRoutePlaceholders($request->route()), $related->getRouteKey()),
'method' => 'PATCH',
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $model)
->visible('update')
->mapToInputs($request, $model),
->authorized($request, $related)
->visible('relation.update')
->mapToInputs($request, $related),
]);
}
}
6 changes: 3 additions & 3 deletions src/Http/Controllers/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ public function show(Request $request, Model $model, Model $related): Response
$field = $request->route('field');

return ResponseFactory::view(
'root::resources.form',
$field->toCreate($request, $model)
'root::resources.show',
$field->toShow($request, $model, $related)
);
}

Expand All @@ -90,7 +90,7 @@ public function edit(Request $request, Model $model, Model $related): Response

return ResponseFactory::view(
'root::resources.form',
$field->toEdit($request, $model)
$field->toEdit($request, $model, $related)
);
}

Expand Down
1 change: 0 additions & 1 deletion src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ public function toShow(Request $request, Model $model): array
'title' => sprintf('%s #%s', $this->getModelName(), $model->getKey()),
'model' => $model,
'action' => $this->modelUrl($model),
'method' => 'PATCH',
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $model)
Expand Down
6 changes: 6 additions & 0 deletions src/RootServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,12 @@ protected function registerRoutes(): void
: $route->parameter('_resource')->resolveRouteBinding($this->app['request'], $id);
});

$this->app['router']->bind('resourceRelation', function (string $id, Route $route): Model {
return $id === 'create'
? $route->parameter('field')->getRelation($route->parameter('resourceModel'))->getRelated()
: $route->parameter('field')->resolveRouteBinding($this->app['request'], $route->parameter('resourceModel'), $id);
});

$this->app['router']
->middleware(['web'])
->domain($root->getDomain())
Expand Down

0 comments on commit 532f8d0

Please sign in to comment.