diff --git a/resources/views/resources/relation.blade.php b/resources/views/resources/relation.blade.php index 7372df7d..7fb14410 100644 --- a/resources/views/resources/relation.blade.php +++ b/resources/views/resources/relation.blade.php @@ -1,3 +1,3 @@ - + @include('root::table.table') diff --git a/resources/views/resources/show.blade.php b/resources/views/resources/show.blade.php index 51c3df16..37d7c033 100644 --- a/resources/views/resources/show.blade.php +++ b/resources/views/resources/show.blade.php @@ -33,7 +33,7 @@ - @if($model->exists) + @if($model->exists && isset($relations))
@foreach($relations as $relation) diff --git a/resources/views/table/table.blade.php b/resources/views/table/table.blade.php index 33141720..820f8b48 100644 --- a/resources/views/table/table.blade.php +++ b/resources/views/table/table.blade.php @@ -48,12 +48,12 @@ class="form-check__control"
@can('view', $row['model']) - + @endcan @can('update', $row['model']) - + @endcan diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index 20982ad1..bbc60e13 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -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); + } } diff --git a/src/Fields/Relation.php b/src/Fields/Relation.php index 0e14d34b..6e504bc7 100644 --- a/src/Fields/Relation.php +++ b/src/Fields/Relation.php @@ -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. */ @@ -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']); } } @@ -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), ]); } } diff --git a/src/Http/Controllers/RelationController.php b/src/Http/Controllers/RelationController.php index ad0946bd..abe16ce3 100644 --- a/src/Http/Controllers/RelationController.php +++ b/src/Http/Controllers/RelationController.php @@ -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) ); } @@ -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) ); } diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index 08f4086d..adfb6ff2 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -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) diff --git a/src/RootServiceProvider.php b/src/RootServiceProvider.php index 51e044e4..bf4aaf65 100644 --- a/src/RootServiceProvider.php +++ b/src/RootServiceProvider.php @@ -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())