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 eb37d13 commit 4dcced6
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 22 deletions.
16 changes: 5 additions & 11 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ public function getRelation(Model $model): EloquentRelation
{
$relation = parent::getRelation($model);

return $relation->withPivot($relation->newPivot()->getKeyName());
return $relation->withPivot([
$relation->newPivot()->getKeyName(),
$relation->getForeignPivotKeyName(),
$relation->getRelatedPivotKeyName(),
]);
}

/**
Expand Down Expand Up @@ -197,16 +201,6 @@ public function mapRelated(Request $request, Model $model, Model $related): arra
return parent::mapRelated($request, $model, $pivot);
}

/**
* {@inheritdoc}
*/
public function handleFormRequest(Request $request, Model $model): void
{
$model = $model instanceof Pivot ? $model : $this->getRelation($model)->newPivot();

parent::handleFormRequest($request, $model);
}

/**
* {@inheritdoc}
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public function resolveFormat(Request $request, Model $model): mixed
$value = $this->resolveDisplay($related);

if (! is_null($resource) && $related->exists && $request->user()->can('view', $related)) {
$value = sprintf('<a href="%s">%s</a>', $resource->modelUrl($related), $value);
$value = sprintf('<a href="%s" data-turbo-frame="_top">%s</a>', $resource->modelUrl($related), $value);
}

return $value;
Expand Down Expand Up @@ -384,7 +384,7 @@ 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();
})->paginate($request->input('per_page'))->withQueryString();
})->paginate($request->input('per_page', 5))->withQueryString();
}

/**
Expand Down
32 changes: 26 additions & 6 deletions src/Http/Controllers/BelongsToManyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,19 @@ public function store(Request $request, Model $model): RedirectResponse
{
$field = $request->route('field');

$field->handleFormRequest($request, $model);
$relation = $field->getRelation($model);

return Redirect::to('')
$pivot = $relation->newPivot([
$relation->getForeignPivotKeyName() => $model->getKey(),
]);

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

$pivot->incrementing = true;

$field->handleFormRequest($request, $pivot);

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

Expand All @@ -30,9 +40,13 @@ public function update(Request $request, Model $model, Model $related): Redirect
{
$field = $request->route('field');

$field->handleFormRequest($request, $model);
$relation = $field->getRelation($model);

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

return Redirect::back()
$field->handleFormRequest($request, $pivot);

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

Expand All @@ -41,9 +55,15 @@ public function update(Request $request, Model $model, Model $related): Redirect
*/
public function destroy(Request $request, Model $model, Model $related): RedirectResponse
{
//
$field = $request->route('field');

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

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

$pivot->delete();

return Redirect::to('')
return Redirect::to($field->modelUrl($model))
->with('alerts.relation-deleted', Alert::success(__('The relation has been deleted!')));
}
}
6 changes: 3 additions & 3 deletions src/Http/Controllers/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function store(Request $request, Model $model): RedirectResponse
$field->handleFormRequest($request, $related);

return Redirect::to(sprintf('%s/s', $field->get))
->with('alerts.relation-created', Alert::success(__('The relation has been created!')));
->with('alerts.relation-created', Alert::success(__('The relation has been created!')));
}

/**
Expand Down Expand Up @@ -91,7 +91,7 @@ public function update(Request $request, Model $model, Model $related): Redirect
$field->handleFormRequest($request, $related);

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

/**
Expand All @@ -104,6 +104,6 @@ public function destroy(Request $request, Model $model, Model $related): Redirec
//

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

0 comments on commit 4dcced6

Please sign in to comment.