diff --git a/.github/workflows/code-style.yml b/.github/workflows/code-style.yml index f9b6f10f..b992c644 100644 --- a/.github/workflows/code-style.yml +++ b/.github/workflows/code-style.yml @@ -19,8 +19,8 @@ jobs: strategy: fail-fast: false matrix: - php: [ 8.2, 8.1 ] - laravel: [ 11.*, 10.* ] + php: [ 8.2 ] + laravel: [ 11.* ] exclude: - php: 8.1 laravel: 11.* diff --git a/.github/workflows/phpstan.yml b/.github/workflows/phpstan.yml index 607f1910..cd3a136b 100644 --- a/.github/workflows/phpstan.yml +++ b/.github/workflows/phpstan.yml @@ -20,7 +20,7 @@ jobs: fail-fast: false matrix: php: [ 8.2 ] - laravel: [ 11.*, 10.* ] + laravel: [ 11.* ] dependency-version: [ prefer-stable ] name: PHP:${{ matrix.php }} / L:${{ matrix.laravel }} diff --git a/resources/views/components/editable.blade.php b/resources/views/components/editable.blade.php index f03e250d..f28a3e7b 100644 --- a/resources/views/components/editable.blade.php +++ b/resources/views/components/editable.blade.php @@ -38,7 +38,7 @@ $params = [ 'theme' => data_get($theme, 'name'), 'tableName' => $tableName, - 'id' => data_get($row, $primaryKey), + 'id' => data_get($row, $this->realPrimaryKey), 'dataField' => $field, 'content' => $content, 'fallback' => $fallback, @@ -74,9 +74,9 @@
@if ($showErrorBag) - @error($field . '.' . $row->{$primaryKey}) + @error($field . '.' . $row->{$this->realPrimaryKey})
- {{ str($message)->replace($field . '.' . $row->{$primaryKey}, $field) }} + {{ str($message)->replace($field . '.' . $row->{$this->realPrimaryKey}, $field) }}
@enderror @endif diff --git a/resources/views/components/frameworks/bootstrap5/editable.blade.php b/resources/views/components/frameworks/bootstrap5/editable.blade.php index 4f9bec84..cb10b876 100644 --- a/resources/views/components/frameworks/bootstrap5/editable.blade.php +++ b/resources/views/components/frameworks/bootstrap5/editable.blade.php @@ -1,6 +1,6 @@
@includeIf(data_get($setUp, 'detail.viewIcon')) diff --git a/resources/views/components/frameworks/bootstrap5/toggleable.blade.php b/resources/views/components/frameworks/bootstrap5/toggleable.blade.php index c7b74726..91a5085d 100644 --- a/resources/views/components/frameworks/bootstrap5/toggleable.blade.php +++ b/resources/views/components/frameworks/bootstrap5/toggleable.blade.php @@ -1,6 +1,6 @@ @php $params = [ - 'id' => data_get($row, $primaryKey), + 'id' => data_get($row, $this->realPrimaryKey), 'isHidden' => !$showToggleable, 'tableName' => $tableName, 'field' => $column->field, diff --git a/resources/views/components/frameworks/tailwind/editable.blade.php b/resources/views/components/frameworks/tailwind/editable.blade.php index 67e0bc55..24ffb43a 100644 --- a/resources/views/components/frameworks/tailwind/editable.blade.php +++ b/resources/views/components/frameworks/tailwind/editable.blade.php @@ -1,6 +1,6 @@
@includeIf(data_get($setUp, 'detail.viewIcon')) diff --git a/resources/views/components/frameworks/tailwind/toggleable.blade.php b/resources/views/components/frameworks/tailwind/toggleable.blade.php index 450d767a..9d7103a9 100644 --- a/resources/views/components/frameworks/tailwind/toggleable.blade.php +++ b/resources/views/components/frameworks/tailwind/toggleable.blade.php @@ -5,7 +5,7 @@ $falseValue = $column->toggleable['default'][1]; $params = [ - 'id' => data_get($row, $primaryKey), + 'id' => data_get($row, $this->realPrimaryKey), 'isHidden' => !$showToggleable, 'tableName' => $tableName, 'field' => $column->field, diff --git a/resources/views/components/row.blade.php b/resources/views/components/row.blade.php index 76bb85cd..f3eb4e84 100644 --- a/resources/views/components/row.blade.php +++ b/resources/views/components/row.blade.php @@ -71,7 +71,7 @@
@if(empty(data_get($row, 'actions')) && $column->isAction) @if (method_exists($this, 'actionsFromView') && $actionsFromView = $this->actionsFromView($row)) -
+
{!! $actionsFromView !!}
@endif @@ -80,7 +80,7 @@ @if (filled(data_get($row, 'actions')) && $column->isAction) @foreach (data_get($row, 'actions') as $key => $action) @if(filled($action)) - + {!! $action !!} @endif @@ -92,7 +92,7 @@ $hasFieldRules = $actionRulesClass->recoverActionForField($row, $field); // =============* Edit On Click *===================== - + $showEditOnClick = false; if (data_get($column->editable, 'hasPermission')) { @@ -125,7 +125,7 @@ $showEditOnClick = false; } @endphp - + @if($showEditOnClick === true) @include(data_get($theme, 'editable.view') ?? null, ['editable' => $column->editable]) diff --git a/resources/views/components/table.blade.php b/resources/views/components/table.blade.php index d0d5b15f..635faf36 100644 --- a/resources/views/components/table.blade.php +++ b/resources/views/components/table.blade.php @@ -29,7 +29,7 @@ @php throw new Exception('To use checkboxes, you must define a unique key attribute in your data source.') @endphp @endif @php - $rowId = data_get($row, $primaryKey); + $rowId = data_get($row, $this->realPrimaryKey); $class = data_get($theme, 'table.trBodyClass'); @@ -96,7 +96,7 @@ realPrimaryKey :$radio :$radioAttribute :$checkbox diff --git a/resources/views/components/table/detail.blade.php b/resources/views/components/table/detail.blade.php index 286c73f0..a3eebc9e 100644 --- a/resources/views/components/table/detail.blade.php +++ b/resources/views/components/table/detail.blade.php @@ -7,20 +7,20 @@ @if (filled($rulesValues['detailView'])) - @includeWhen(data_get($setUp, 'detail.state.' . $row->{$primaryKey}), + @includeWhen(data_get($setUp, 'detail.state.' . $row->{$this->realPrimaryKey}), $rulesValues['detailView'][0]['detailView'], [ - 'id' => data_get($row, $primaryKey), + 'id' => data_get($row, $this->realPrimaryKey), 'options' => array_merge( data_get($setUp, 'detail.options'), $rulesValues['detailView']['0']['options']), ] ) @else - @includeWhen(data_get($setUp, 'detail.state.' . $row->{$primaryKey}), + @includeWhen(data_get($setUp, 'detail.state.' . $row->{$this->realPrimaryKey}), data_get($setUp, 'detail.view'), [ - 'id' => data_get($row, $primaryKey), + 'id' => data_get($row, $this->realPrimaryKey), 'options' => data_get($setUp, 'detail.options'), ] ) diff --git a/resources/views/livewire/lazy-child.blade.php b/resources/views/livewire/lazy-child.blade.php index f79980ee..682c5a03 100644 --- a/resources/views/livewire/lazy-child.blade.php +++ b/resources/views/livewire/lazy-child.blade.php @@ -4,7 +4,7 @@ @foreach ($data as $row) @php - $rowId = data_get($row, $primaryKey); + $rowId = data_get($row, $this->realPrimaryKey); $class = data_get($theme, 'table.trBodyClass'); diff --git a/src/Components/Actions/Macros.php b/src/Components/Actions/Macros.php index 35c343c9..4d6399e6 100644 --- a/src/Components/Actions/Macros.php +++ b/src/Components/Actions/Macros.php @@ -80,7 +80,7 @@ public static function boot(): void "component" => "button", "attribute" => "wire:click", "value" => function ($component, $row) { - return 'toggleDetail(\'' . data_get($row, $component->primaryKey) . '\')'; + return 'toggleDetail(\'' . data_get($row, $component->realPrimaryKey) . '\')'; }, ]; diff --git a/src/Concerns/Base.php b/src/Concerns/Base.php index c31ea2be..5513c28f 100644 --- a/src/Concerns/Base.php +++ b/src/Concerns/Base.php @@ -2,12 +2,15 @@ namespace PowerComponents\LivewirePowerGrid\Concerns; +use Livewire\Attributes\Computed; use PowerComponents\LivewirePowerGrid\ProcessDataSource; trait Base { public string $primaryKey = 'id'; + public ?string $primaryKeyAlias = null; + public bool $ignoreTablePrefix = true; public string $tableName = 'default'; @@ -44,6 +47,12 @@ trait Base public int $totalCurrentPage = 0; + #[Computed] + public function realPrimaryKey(): string + { + return $this->primaryKeyAlias ?? $this->primaryKey; + } + public function template(): ?string { return null; diff --git a/src/Concerns/ToggleDetail.php b/src/Concerns/ToggleDetail.php index f6e08605..f670bcdd 100644 --- a/src/Concerns/ToggleDetail.php +++ b/src/Concerns/ToggleDetail.php @@ -29,7 +29,9 @@ private function resolveDetailRow(mixed $results): void $collection = !is_array($results) && method_exists($results, 'items') ? collect($results->items()) : collect($results); } - $collection->pluck($this->primaryKey) + $primaryKey = $this->primaryKeyAlias ?? $this->primaryKey; + + $collection->pluck($primaryKey) ->map(fn ($id) => strval($id)) ->each(function ($id) { $state = data_get($this->setUp, "detail.state.$id", false); diff --git a/src/Providers/PowerGridServiceProvider.php b/src/Providers/PowerGridServiceProvider.php index 8ce0a910..524ae5c5 100644 --- a/src/Providers/PowerGridServiceProvider.php +++ b/src/Providers/PowerGridServiceProvider.php @@ -110,7 +110,7 @@ private function macros(): void $perPage = $perPage ?: $this->model->getPerPage(); - $results = $this->model->newCollection( + $results = $this->model->newCollection( //@phpstan-ignore-line $engine->map( $this, $rawResults = $engine->paginate($this, $perPage, $page), diff --git a/tests/Feature/Buttons/ToggleDetailTest.php b/tests/Feature/Buttons/ToggleDetailTest.php index d41b0e04..fad70516 100644 --- a/tests/Feature/Buttons/ToggleDetailTest.php +++ b/tests/Feature/Buttons/ToggleDetailTest.php @@ -1,8 +1,11 @@ [$component::class, (object) ['theme' => 'tailwind', 'join' => false]], - 'bootstrap' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => false]], - 'tailwind join' => [$component::class, (object) ['theme' => 'tailwind', 'join' => true]], - 'bootstrap join' => [$component::class, (object) ['theme' => 'bootstrap', 'join' => true]], -]); +$dotNotationPrimaryKeyComponent = new class () extends DishTableBase { + public string $primaryKey = 'dishes.id'; + + public ?string $primaryKeyAlias = 'id'; + + public function dataSource(): Builder + { + return Dish::query() + ->join('categories as newCategories', function ($categories) { + $categories->on('dishes.category_id', '=', 'newCategories.id'); + }) + ->select('dishes.*', 'newCategories.name as category_name'); + } + + public function actions($row): array + { + return [ + Button::make('toggleDetail') + ->slot('toggleDetail: ' . $row->id) + ->toggleDetail(), + ]; + } +}; it('properly displays "toggleDetail" on edit button', function (string $component, object $params) { - livewire($component, [ - 'join' => $params->join, - ]) + livewire($component) ->call($params->theme) ->set('setUp.footer.perPage', 6) ->assertSeeHtml("wire:click=\"toggleDetail('1')\">toggleDetail: 1") @@ -36,5 +54,25 @@ public function actions($row): array ->assertSeeHtml("wire:click=\"toggleDetail('7')\">toggleDetail: 7") ->assertDontSeeHtml("wire:click=\"toggleDetail('1')\">toggleDetail: 1"); }) - ->with('action:toggleDetail') + ->with([ + 'tailwind' => [$component::class, (object) ['theme' => 'tailwind']], + 'bootstrap' => [$component::class, (object) ['theme' => 'bootstrap']], + ]) + ->group('action'); + +it('properly displays "toggleDetail" on edit button using dot notation in primaryKey', function (string $component, object $params) { + livewire($component) + ->call($params->theme) + ->set('setUp.footer.perPage', 6) + ->assertSeeHtml("\"toggleDetail('1')\">toggleDetail: 1") + ->assertSeeHtml("\"toggleDetail('2')\">toggleDetail: 2") + ->assertDontSeeHtml("wire:click=\"toggleDetail('7')\">toggleDetail: 7") + ->call('setPage', 2) + ->assertSeeHtml("wire:click=\"toggleDetail('7')\">toggleDetail: 7") + ->assertDontSeeHtml("wire:click=\"toggleDetail('1')\">toggleDetail: 1"); +}) + ->with([ + 'tailwind join primary key alias' => [$dotNotationPrimaryKeyComponent::class, (object) ['theme' => 'tailwind']], + 'bootstrap join primary key alias' => [$dotNotationPrimaryKeyComponent::class, (object) ['theme' => 'bootstrap']], + ]) ->group('action'); diff --git a/tests/Feature/DetailRowTest.php b/tests/Feature/DetailRowTest.php index 2eb676f9..69cf6f16 100644 --- a/tests/Feature/DetailRowTest.php +++ b/tests/Feature/DetailRowTest.php @@ -110,7 +110,7 @@ } $component->assertSeeHtmlInOrder($xData); -})->skip(); +}); it('collapse detail row with collapseOthers', function () { livewire(RulesToggleDetailTable::class, [ @@ -184,4 +184,4 @@ 4 => false, 5 => false, ]); -})->skip(); +});