Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Jun 26, 2024
1 parent 2ddb88c commit 1c2dff9
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 14 deletions.
12 changes: 8 additions & 4 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public function fields(Request $request): array
$this->getRelation($model->pivotParent)->select($query->getModel()->getQualifiedKeyName())
);
});
})->hydrate(function (Request $request, Pivot $model, mixed $value): void {
$relation = $this->getRelation($model->pivotParent);

$model->setAttribute($relation->getRelatedPivotKeyName(), $value);
})->display(function (Model $model): ?string {
return $this->resolveDisplay($model);
}),
Expand Down Expand Up @@ -249,11 +253,11 @@ public function routes(Router $router): void
if ($this->isSubResource()) {
$router->get('/', [BelongsToManyController::class, 'index']);
$router->get('/create', [BelongsToManyController::class, 'create']);
$router->get("/{{$this->getRouteKeyName()}", [BelongsToManyController::class, 'show']);
$router->get("/{{$this->getRouteKeyName()}}", [BelongsToManyController::class, 'show']);
$router->post('/', [BelongsToManyController::class, 'store']);
$router->get("/{{$this->getRouteKeyName()}/edit", [BelongsToManyController::class, 'edit']);
$router->patch("/{{$this->getRouteKeyName()}", [BelongsToManyController::class, 'update']);
$router->delete("/{{$this->getRouteKeyName()}", [BelongsToManyController::class, 'destroy']);
$router->get("/{{$this->getRouteKeyName()}}/edit", [BelongsToManyController::class, 'edit']);
$router->patch("/{{$this->getRouteKeyName()}}", [BelongsToManyController::class, 'update']);
$router->delete("/{{$this->getRouteKeyName()}}", [BelongsToManyController::class, 'destroy']);
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/Fields/MorphToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public function fields(Request $request): array
$this->getRelation($model->pivotParent)->select($query->getModel()->getQualifiedKeyName())
);
});
})->hydrate(function (Request $request, MorphPivot $model): void {
$relation = $this->getRelation($model->pivotParent);

$model->setAttribute(
$relation->getRelatedPivotKeyName(),
$model->related->getAttribute($relation->getRelatedKeyName())
);
})->display(function (Model $model): mixed {
return $this->resolveDisplay($model);
}),
Expand Down
19 changes: 12 additions & 7 deletions src/Fields/Relation.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Illuminate\Database\Eloquent\Relations\Relation as EloquentRelation;
use Illuminate\Http\Request;
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -605,13 +606,17 @@ public function paginate(Request $request, Model $model): LengthAwarePaginator
{
$relation = $this->getRelation($model);

return $this->resolveFilters($request)
->apply($request, $relation->getQuery())
$this->resolveFilters($request)->apply($request, $relation->getQuery());

return $relation
->with($this->with)
->withCount($this->withCount)
->latest()
->paginate($request->input($this->getPerPageKey(), $request->isTurboFrameRequest() ? 5 : $relation->getRelated()->getPerPage()))
->withQueryString();
->paginate(
$request->input($this->getPerPageKey(), $request->isTurboFrameRequest()
? 5
: $relation->getRelated()->getPerPage())
)->withQueryString();
}

/**
Expand Down Expand Up @@ -838,10 +843,10 @@ public function routes(Router $router): void
*/
public function registerRouteConstraints(Request $request, Router $router): void
{
$router->bind($this->getRouteKeyName(), function (string $id) use ($request): Model {
$router->bind($this->getRouteKeyName(), function (string $id, Route $route) use ($router): Model {
return $id === 'create'
? $this->getRelation($request->route()->parentOfParameter($this->getRouteKeyName()))->make()
: $this->resolveRouteBinding($request, $id);
? $this->getRelation($route->parentOfParameter($this->getRouteKeyName()))->make()
: $this->resolveRouteBinding($router->getCurrentRequest(), $id);
});
}

Expand Down
41 changes: 39 additions & 2 deletions tests/Http/BelongsToManyControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class BelongsToManyControllerTest extends TestCase
{
protected User $admin;

protected Team $team;

protected BelongsToMany $field;

public function setUp(): void
Expand All @@ -20,6 +22,12 @@ public function setUp(): void

$this->admin = User::factory()->create();

$this->team = Team::factory()->create();

$this->admin->teams()->attach([
$this->team->getKey() => ['role' => 'admin'],
]);

$this->field = Root::instance()
->resources
->resolve('users')
Expand All @@ -45,15 +53,44 @@ public function test_a_belongs_to_many_controller_handles_store(): void
$this->actingAs($this->admin)
->post('/root/users/'.$this->admin->getKey().'/fields/teams', [
'related' => $team->getKey(),
'role' => 'admin',
'role' => 'member',
])
->assertRedirect()
->assertSessionHas('alerts.relation-created');

$this->assertDatabaseHas('team_user', [
'user_id' => $this->admin->getKey(),
'team_id' => $team->getKey(),
'role' => 'admin',
'role' => 'member',
]);
}

public function test_a_belongs_to_many_controller_handles_update(): void
{
$team = $this->admin->teams->first();

$this->assertSame('admin', $team->pivot->role);

$this->actingAs($this->admin)
->patch('/root/users/'.$this->admin->getKey().'/fields/teams/'.$team->pivot->getKey(), [
'related' => $team->getKey(),
'role' => 'member',
])
->assertRedirect()
->assertSessionHas('alerts.relation-updated');

$this->assertSame('member', $team->pivot->refresh()->role);
}

public function test_a_belongs_to_many_controller_handles_destroy(): void
{
$team = $this->admin->teams->first();

$this->actingAs($this->admin)
->delete('/root/users/'.$this->admin->getKey().'/fields/teams/'.$team->pivot->getKey())
->assertRedirect()
->assertSessionHas('alerts.relation-deleted');

$this->assertDatabaseMissing('team_user', ['id' => $team->pivot->getKey()]);
}
}
4 changes: 3 additions & 1 deletion tests/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class User extends Model implements MustVerifyEmail

protected $guarded = [];

protected $table = 'users';

protected static function newFactory(): UserFactory
{
return new class() extends UserFactory
Expand Down Expand Up @@ -49,6 +51,6 @@ public function documents(): MorphToMany
public function teams(): BelongsToMany
{
return $this->belongsToMany(Team::class)
->withPivot('role');
->withPivot(['id', 'role']);
}
}

0 comments on commit 1c2dff9

Please sign in to comment.