diff --git a/src/Fields/Repeater.php b/src/Fields/Repeater.php index 21cc3fe9..c4a4f3ab 100644 --- a/src/Fields/Repeater.php +++ b/src/Fields/Repeater.php @@ -218,13 +218,13 @@ public function modelUrl(Model $model): string public function resolveFormat(Request $request, Model $model): ?string { if (is_null($this->formatResolver)) { - $this->formatResolver = function (Request $request, Model $model, array $value): string { + $this->formatResolver = function (Request $request, Model $model, ?array $value = null): string { $values = array_map(function (array $value) use ($request, $model): array { return $this->resolveOptionFields($request, $model, $this->newTemporaryModel($value)) ->authorized($request, $model) ->visible('show') ->mapToDisplay($request, $model); - }, $value); + }, (array) $value); return View::make('root::fields.repeater-table', ['values' => $values])->render(); }; diff --git a/tests/Http/MorphToControllerTest.php b/tests/Http/MorphToControllerTest.php index 48f1f3bd..c403843f 100644 --- a/tests/Http/MorphToControllerTest.php +++ b/tests/Http/MorphToControllerTest.php @@ -28,7 +28,7 @@ public function setUp(): void $this->admin = User::factory()->create(); } - public function test_a_relation_controller_handles_request(): void + public function test_a_morph_to_controller_handles_request(): void { $this->actingAs($this->admin) ->get('/root/users/'.$this->admin->getKey().'/fields/employer') diff --git a/tests/Http/RepeaterControllerTest.php b/tests/Http/RepeaterControllerTest.php new file mode 100644 index 00000000..612d084d --- /dev/null +++ b/tests/Http/RepeaterControllerTest.php @@ -0,0 +1,37 @@ +field = Root::instance() + ->resources + ->resolve('users') + ->resolveFields($this->app['request']) + ->first(function ($field) { + return $field->getModelAttribute() === 'settings'; + }); + + $this->admin = User::factory()->create(); + } + + public function test_a_repeater_controller_handles_request(): void + { + $this->actingAs($this->admin) + ->post('/root/users/'.$this->admin->getKey().'/fields/settings') + ->assertOk(); + } +} diff --git a/tests/Resources/UserResource.php b/tests/Resources/UserResource.php index 93d1f408..774d3872 100644 --- a/tests/Resources/UserResource.php +++ b/tests/Resources/UserResource.php @@ -8,6 +8,7 @@ use Cone\Root\Fields\ID; use Cone\Root\Fields\Media; use Cone\Root\Fields\MorphTo; +use Cone\Root\Fields\Repeater; use Cone\Root\Fields\Select; use Cone\Root\Fields\Text; use Cone\Root\Resources\Resource; @@ -61,7 +62,6 @@ public function fields(Request $request): array ]), ]; }), - MorphTo::make('Employer') ->display('name') ->nullable() @@ -69,6 +69,13 @@ public function fields(Request $request): array User::class, Team::class, ]), + Repeater::make('Settings') + ->withFields(function () { + return [ + Text::make('Key'), + Text::make('Value'), + ]; + }), ]; } diff --git a/tests/User.php b/tests/User.php index a10c287f..ec9dc936 100644 --- a/tests/User.php +++ b/tests/User.php @@ -33,6 +33,7 @@ public function definition(): array 'employer_id' => null, 'employer_type' => null, 'deleted_at' => null, + 'settings' => null, ]); } }; @@ -41,10 +42,9 @@ public function definition(): array protected function casts(): array { return [ - 'created_at' => 'datetime', 'email_verified_at' => 'datetime', 'password' => 'hashed', - 'updated_at' => 'datetime', + 'settings' => 'json', ]; } diff --git a/tests/migrations/2024_06_14_213755_alter_users_table.php b/tests/migrations/2024_06_14_213755_alter_users_table.php index b92f5965..c14e6b83 100644 --- a/tests/migrations/2024_06_14_213755_alter_users_table.php +++ b/tests/migrations/2024_06_14_213755_alter_users_table.php @@ -15,6 +15,9 @@ public function up(): void $table->after('id', function (Blueprint $table) { $table->nullableMorphs('employer'); }); + $table->after('password', function (Blueprint $table) { + $table->json('settings')->nullable(); + }); $table->after('updated_at', function (Blueprint $table) { $table->softDeletes(); }); @@ -28,6 +31,7 @@ public function down(): void { Schema::table('users', function (Blueprint $table) { $table->dropMorphs('employer'); + $table->dropColumn('settings'); $table->dropSoftDeletes(); }); }