Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 20, 2023
1 parent 532f8d0 commit 91d4cf3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 43 deletions.
2 changes: 1 addition & 1 deletion resources/views/components/layout/notifications.blade.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<div class="app-notification" x-data="notifications('{{ $url }}')" x-on:click.outside="open = false">
<div id="notifications" class="app-notification" x-data="notifications('{{ $url }}')" x-on:click.outside="open = false" data-turbo-permanent>
<button
class="btn btn--light btn--icon btn--counter app-notification__toggle"
aria-label="{{ __('Notifications') }}"
Expand Down
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 }}" data-turbo-action="advance">
<turbo-frame id="relation-{{ $attribute }}">
@include('root::table.table')
</turbo-frame>
38 changes: 37 additions & 1 deletion src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ public function resolveHydrate(Request $request, Model $model, mixed $value): vo
parent::resolveHydrate($request, $model, $value);
}

/**
* {@inheritdoc}
*/
public function resolveRouteBinding(Request $request, Model $model, string $id): Model
{
$relation = $this->getRelation($model);

return $relation->wherePivot($relation->qualifyPivotColumn($relation->newPivot()->getRouteKeyName()), $id)->firstOrFail();
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -226,7 +236,19 @@ public function toValidate(Request $request, Model $model): array
}

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

$pivot = $relation->newPivot();

return parent::toShow($request, $model, $pivot);
}

/**
* Get the show representation of the relation.
*/
public function toShow(Request $request, Model $model, Model $related): array
{
Expand All @@ -238,4 +260,18 @@ public function toShow(Request $request, Model $model, Model $related): array

return parent::toShow($request, $model, $pivot);
}

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

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

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

return parent::toEdit($request, $model, $pivot);
}
}
5 changes: 4 additions & 1 deletion src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,10 @@ public function routes(Router $router): void
$router->get('/', [RelationController::class, 'index']);
$router->get('/create', [RelationController::class, 'create']);
$router->get('/{resourceRelation}', [RelationController::class, 'show']);
$router->post('/', [RelationController::class, 'store']);
$router->get('/{resourceRelation}/edit', [RelationController::class, 'edit']);
$router->patch('/{resourceRelation}', [RelationController::class, 'update']);
$router->delete('/{resourceRelation}', [RelationController::class, 'destroy']);
}
}

Expand Down Expand Up @@ -538,7 +541,7 @@ 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->getRelatedName(), $related->getKey())]),
'model' => $related,
'url' => sprintf('%s/%s', $this->replaceRoutePlaceholders($request->route()), $related->getRouteKey()),
'action' => sprintf('%s/%s', $this->replaceRoutePlaceholders($request->route()), $related->getRouteKey()),
'method' => 'PATCH',
'fields' => $this->resolveFields($request)
->subResource(false)
Expand Down
45 changes: 6 additions & 39 deletions src/Http/Controllers/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Cone\Root\Support\Alert;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
Expand Down Expand Up @@ -46,25 +45,9 @@ public function create(Request $request, Model $model): Response
*/
public function store(Request $request, Model $model): RedirectResponse
{
$field = $request->resolved();
//

$relation = $field->getRelation($model);

$related = tap($relation->getRelated(), static function (Model $related) use ($model): void {
$related->setRelation('parent', $model);
});

$fields = $field->resolveFields($request)->available($request, $model, $related);

$request->validate($fields->mapToValidate($request, $related));

$fields->each->persist($request, $related);

$relation->save($related);

$path = sprintf('%s/%s', $request->resolved()->resolveUri($request), $related->getKey());

return Redirect::to($path)
return Redirect::to('')
->with('alerts.relation-created', Alert::success(__('The relation has been created!')));
}

Expand Down Expand Up @@ -99,21 +82,9 @@ public function edit(Request $request, Model $model, Model $related): Response
*/
public function update(Request $request, Model $model, Model $related): RedirectResponse
{
$field = $request->resolved();

$related->setRelation('parent', $model);
//

$fields = $field->resolveFields($request)->available($request, $model, $related);

$request->validate($fields->mapToValidate($request, $related));

$fields->each->persist($request, $related);

$related->save();

$path = sprintf('%s/%s/edit', $request->resolved()->resolveUri($request), $related->getKey());

return Redirect::to($path)
return Redirect::back()
->with('alerts.relation-updated', Alert::success(__('The relation has been updated!')));
}

Expand All @@ -122,13 +93,9 @@ public function update(Request $request, Model $model, Model $related): Redirect
*/
public function destroy(Request $request, Model $model, Model $related): RedirectResponse
{
$trashed = class_uses_recursive(SoftDeletes::class) && $related->trashed();

$trashed ? $related->forceDelete() : $related->delete();

$path = $request->resolved()->resolveUri($request);
//

return Redirect::to($path)
return Redirect::to('')
->with('alerts.relation-deleted', Alert::success(__('The relation has been deleted!')));
}
}

0 comments on commit 91d4cf3

Please sign in to comment.