Skip to content

Commit

Permalink
Merge pull request #13963 from filamentphp/fix/json-wildcard-table-co…
Browse files Browse the repository at this point in the history
…lumn-state

feature: JSON wildcard (*) table column state
  • Loading branch information
danharrin authored Aug 16, 2024
2 parents 2455d28 + fe32c7c commit 8de1a8d
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
3 changes: 1 addition & 2 deletions packages/support/src/Concerns/HasCellState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions tests/database/migrations/create_posts_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
1 change: 1 addition & 0 deletions tests/src/Models/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Post extends Model
protected $casts = [
'is_published' => 'boolean',
'tags' => 'array',
'json_array_of_objects' => 'array',
];

protected $guarded = [];
Expand Down
13 changes: 13 additions & 0 deletions tests/src/Tables/ColumnTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
1 change: 1 addition & 0 deletions tests/src/Tables/Fixtures/PostsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 8de1a8d

Please sign in to comment.