From 903ca8a03c7d8672bf19e12f0e257c5ee894bbf3 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Fri, 16 Aug 2024 11:26:04 +0100 Subject: [PATCH 1/2] feature: JSON wildcard (*) table column state --- packages/support/src/Concerns/HasCellState.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/support/src/Concerns/HasCellState.php b/packages/support/src/Concerns/HasCellState.php index ec409ac733e..fb1715d40b8 100644 --- a/packages/support/src/Concerns/HasCellState.php +++ b/packages/support/src/Concerns/HasCellState.php @@ -8,7 +8,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\Relation; -use Illuminate\Support\Arr; use Illuminate\Support\Collection; use Illuminate\Support\Stringable; use Znck\Eloquent\Relations\BelongsToThrough; @@ -98,7 +97,7 @@ public function getStateFromRecord(): mixed { $record = $this->getRecord(); - $state = Arr::get($record, $this->getName()); + $state = data_get($record, $this->getName()); if ($state !== null) { return $state; From fe32c7c61cae3bb80fda63277d537bfcad276bc5 Mon Sep 17 00:00:00 2001 From: Dan Harrin Date: Fri, 16 Aug 2024 11:59:57 +0100 Subject: [PATCH 2/2] add test --- tests/database/migrations/create_posts_table.php | 1 + tests/src/Models/Post.php | 1 + tests/src/Tables/ColumnTest.php | 13 +++++++++++++ tests/src/Tables/Fixtures/PostsTable.php | 1 + 4 files changed, 16 insertions(+) diff --git a/tests/database/migrations/create_posts_table.php b/tests/database/migrations/create_posts_table.php index be3ddc399dd..361458285f8 100644 --- a/tests/database/migrations/create_posts_table.php +++ b/tests/database/migrations/create_posts_table.php @@ -16,6 +16,7 @@ public function up(): void $table->unsignedTinyInteger('rating')->default(0); $table->json('tags')->nullable(); $table->string('title'); + $table->json('json_array_of_objects')->nullable(); $table->timestamps(); $table->softDeletes(); }); diff --git a/tests/src/Models/Post.php b/tests/src/Models/Post.php index 1a7f2453c3f..f98b494a2d1 100644 --- a/tests/src/Models/Post.php +++ b/tests/src/Models/Post.php @@ -16,6 +16,7 @@ class Post extends Model protected $casts = [ 'is_published' => 'boolean', 'tags' => 'array', + 'json_array_of_objects' => 'array', ]; protected $guarded = []; diff --git a/tests/src/Tables/ColumnTest.php b/tests/src/Tables/ColumnTest.php index 9ca1b08d716..e6d5c2e7d13 100644 --- a/tests/src/Tables/ColumnTest.php +++ b/tests/src/Tables/ColumnTest.php @@ -141,6 +141,19 @@ ->assertTableColumnFormattedStateNotSet('formatted_state', 'incorrect formatted state', $post); }); +it('can output values in a JSON array column of objects', function () { + $post = Post::factory()->create([ + 'json_array_of_objects' => [ + ['value' => 'foo'], + ['value' => 'bar'], + ['value' => 'baz'], + ], + ]); + + livewire(PostsTable::class) + ->assertTableColumnStateSet('json_array_of_objects.*.value', ['foo', 'bar', 'baz'], $post); +}); + it('can state whether a column has extra attributes', function () { $post = Post::factory()->create(); diff --git a/tests/src/Tables/Fixtures/PostsTable.php b/tests/src/Tables/Fixtures/PostsTable.php index af1b48493b5..677345dc5a2 100644 --- a/tests/src/Tables/Fixtures/PostsTable.php +++ b/tests/src/Tables/Fixtures/PostsTable.php @@ -72,6 +72,7 @@ public function table(Table $table): Table ->state('correct state'), Tables\Columns\TextColumn::make('formatted_state') ->formatStateUsing(fn () => 'formatted state'), + Tables\Columns\TextColumn::make('json_array_of_objects.*.value'), Tables\Columns\TextColumn::make('extra_attributes') ->extraAttributes([ 'class' => 'text-danger-500',