Skip to content

Commit

Permalink
add show for resources
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 4, 2023
1 parent 5c998a9 commit b65b2a6
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion resources/views/components/layout/sidebar-group.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class="btn btn--light btn--sm btn--icon block-navigation__toggle"
<ul>
@foreach($items as $item)
<li>
<a href="{{ $item->url }}" @if($item->matched()) aria-current="page" @endif>
<a href="{{ $item->url }}" @if($item->partiallyMatched()) aria-current="page" @endif>
@if($item->icon)
<x-root::icon :name="$item->icon" class="icon" />
@endif
Expand Down
7 changes: 6 additions & 1 deletion resources/views/resources/table/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ class="form-check__control"
@endforeach
<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') }}">
<x-root::icon name="eye" class="btn__icon" />
</a>
@endcan
@can('update', $row['model'])
<a href="{{ $row['url'] }}" 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') }}">
<x-root::icon name="edit" class="btn__icon" />
</a>
@endcan
Expand Down
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
Route::get('/{resource}', [ResourceController::class, 'index'])->name('resource.index');
Route::get('/{resource}/create', [ResourceController::class, 'create'])->name('resource.create');
Route::post('/{resource}', [ResourceController::class, 'store'])->name('resource.store');
Route::get('/{resource}/{resourceModel}', [ResourceController::class, 'edit'])->name('resource.edit');
Route::get('/{resource}/{resourceModel}', [ResourceController::class, 'show'])->name('resource.show');
Route::get('/{resource}/{resourceModel}/edit', [ResourceController::class, 'edit'])->name('resource.edit');
Route::patch('/{resource}/{resourceModel}', [ResourceController::class, 'update'])->name('resource.update');
Route::delete('/{resource}/{resourceModel}', [ResourceController::class, 'destroy'])->name('resource.delete');
Route::post('/{resource}/{resourceModel}/restore', [ResourceController::class, 'restore'])->name('resource.restore');
15 changes: 15 additions & 0 deletions src/Http/Controllers/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ public function store(Request $request, Resource $resource): RedirectResponse
->with('alerts.resource-created', Alert::success(__('The resource has been created!')));
}

/**
* Display the specified resource.
*/
public function show(Request $request, Resource $resource, Model $model): Response
{
if ($resource->getPolicy()) {
$this->authorize('view', $model);
}

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

/**
* Show the form for editing the specified resource.
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,23 @@ public function toCreate(Request $request): array
]);
}

/**
* Get the edit representation of the resource.
*/
public function toShow(Request $request, Model $model): array
{
return array_merge($this->toArray(), [
'title' => sprintf('%s #%s', $this->getModelName(), $model->getKey()),
'model' => $model,
'action' => $this->modelUrl($model),
'method' => 'PATCH',
'fields' => $this->resolveFields($request)
->authorized($request, $model)
->visible('show')
->mapToDisplay($request, $model),
]);
}

/**
* Get the edit representation of the resource.
*/
Expand Down

0 comments on commit b65b2a6

Please sign in to comment.