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 91d4cf3 commit 8c593c8
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 8 deletions.
36 changes: 35 additions & 1 deletion src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
namespace Cone\Root\Fields;

use Closure;
use Cone\Root\Http\Controllers\BelongsToManyController;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo as BelongsToRelation;
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentRelation;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Arr;

class BelongsToMany extends Relation
Expand Down Expand Up @@ -170,7 +172,13 @@ public function resolveRouteBinding(Request $request, Model $model, string $id):
{
$relation = $this->getRelation($model);

return $relation->wherePivot($relation->qualifyPivotColumn($relation->newPivot()->getRouteKeyName()), $id)->firstOrFail();
$related = $relation->wherePivot($relation->newPivot()->getQualifiedKeyName(), $id)->firstOrFail();

return tap($related, static function (Model $related) use ($relation, $id): void {
$pivot = $related->getRelation($relation->getPivotAccessor());

$pivot->setRelation('related', $related)->setAttribute($pivot->getKeyName(), $id);
});
}

/**
Expand All @@ -187,6 +195,32 @@ 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}
*/
public function routes(Router $router): void
{
if ($this->isSubResource()) {
$router->get('/', [BelongsToManyController::class, 'index']);
$router->get('/create', [BelongsToManyController::class, 'create']);
$router->get('/{resourceRelation}', [BelongsToManyController::class, 'show']);
$router->post('/', [BelongsToManyController::class, 'store']);
$router->get('/{resourceRelation}/edit', [BelongsToManyController::class, 'edit']);
$router->patch('/{resourceRelation}', [BelongsToManyController::class, 'update']);
$router->delete('/{resourceRelation}', [BelongsToManyController::class, 'destroy']);
}
}

/**
* {@inheritdoc}
*/
Expand Down
8 changes: 4 additions & 4 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public function mapRelated(Request $request, Model $model, Model $related): arra
{
return [
'id' => $related->getKey(),
'url' => sprintf('%s/%s', $this->replaceRoutePlaceholders($request->route()), $related->getRouteKey()),
'url' => sprintf('%s/%s', $this->replaceRoutePlaceholders($request->route()), $related->getKey()),
'model' => $related,
'fields' => $this->resolveFields($request)
->subResource(false)
Expand Down Expand Up @@ -502,7 +502,7 @@ public function toCreate(Request $request, Model $model): array
return array_merge($this->toSubResource($request, $model), [
'title' => __('Create :model', ['model' => $this->getRelatedName()]),
'model' => $related = $this->getRelation($model)->getRelated(),
'action' => $this->replaceRoutePlaceholders($request->route()), $related->getRouteKey(),
'action' => $this->replaceRoutePlaceholders($request->route()), $related->getKey(),
'method' => 'POST',
'fields' => $this->resolveFields($request)
->subResource(false)
Expand All @@ -520,7 +520,7 @@ 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()),
'action' => sprintf('%s/%s', $this->replaceRoutePlaceholders($request->route()), $related->getKey()),
'fields' => $this->resolveFields($request)
->subResource(false)
->authorized($request, $related)
Expand All @@ -541,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,
'action' => sprintf('%s/%s', $this->replaceRoutePlaceholders($request->route()), $related->getRouteKey()),
'action' => sprintf('%s/%s', $this->replaceRoutePlaceholders($request->route()), $related->getKey()),
'method' => 'PATCH',
'fields' => $this->resolveFields($request)
->subResource(false)
Expand Down
49 changes: 49 additions & 0 deletions src/Http/Controllers/BelongsToManyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

Check warning on line 1 in src/Http/Controllers/BelongsToManyController.php

View workflow job for this annotation

GitHub Actions / 4️⃣ Coding Standards

Found violation(s) of type: method_chaining_indentation

namespace Cone\Root\Http\Controllers;

use Cone\Root\Support\Alert;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;

class BelongsToManyController extends RelationController
{
/**
* Store a newly created resource in storage.
*/
public function store(Request $request, Model $model): RedirectResponse
{
$field = $request->route('field');

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

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

/**
* Update the specified resource in storage.
*/
public function update(Request $request, Model $model, Model $related): RedirectResponse
{
$field = $request->route('field');

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

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

/**
* Remove the specified resource from storage.
*/
public function destroy(Request $request, Model $model, Model $related): RedirectResponse
{
//

return Redirect::to('')
->with('alerts.relation-deleted', Alert::success(__('The relation has been deleted!')));
}
}
10 changes: 8 additions & 2 deletions src/Http/Controllers/RelationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ public function create(Request $request, Model $model): Response
*/
public function store(Request $request, Model $model): RedirectResponse
{
//
$field = $request->route('field');

// $field->handleFormRequest($request, $model);

return Redirect::to('')
->with('alerts.relation-created', Alert::success(__('The relation has been created!')));
Expand Down Expand Up @@ -82,7 +84,9 @@ public function edit(Request $request, Model $model, Model $related): Response
*/
public function update(Request $request, Model $model, Model $related): RedirectResponse
{
//
$field = $request->route('field');

// $field->handleFormRequest($request, $model);

return Redirect::back()
->with('alerts.relation-updated', Alert::success(__('The relation has been updated!')));
Expand All @@ -93,6 +97,8 @@ public function update(Request $request, Model $model, Model $related): Redirect
*/
public function destroy(Request $request, Model $model, Model $related): RedirectResponse
{
$field = $request->route('field');

//

return Redirect::to('')
Expand Down
2 changes: 1 addition & 1 deletion src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function isSoftDeletable(): bool
*/
public function modelUrl(Model $model): string
{
return sprintf('%s/%s', $this->getUri(), $model->exists ? $model->getRouteKey() : '');
return sprintf('%s/%s', $this->getUri(), $model->exists ? $model->getKey() : '');
}

/**
Expand Down

0 comments on commit 8c593c8

Please sign in to comment.