filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
19.29k stars 2.96k forks source link

Issues with $record in visible() closure on Tables\Columns\TextColumn: Filament is unable to resolve the record #14539

Closed giovanniledda closed 1 month ago

giovanniledda commented 1 month ago

Package

filament/filament

Package Version

3.2.82

Laravel Version

11.8.0

Livewire Version

3.5.0

PHP Version

8.2.22

Problem description

I have a table with simple TextColumn, so defined:

           Tables\Columns\TextColumn::make('order_suppliers_to_be_approved_by_shipyard')
                ->visible(function (Model $record) {
                    return auth()->user()?->isShipyard() || (auth()->user()?->isSupplier() && auth()->user()->supplier->isLeaderSupplierForOrder($record->id));
                })
                ->label(__('Order suppliers to be approved by shipyard'))
                ->state(function (Model $record) {
                    return $record->orderSuppliersToBeApprovedByShipyard()->count();
                })
                ->color(fn ($state) => $state != 0 ? 'warning' : '')
                ->icon(fn ($state) => $state != 0 ? 'heroicon-m-exclamation-triangle' : ''),

The $record is not found, here the error that I get instead:

App\Filament\Resources\OrderResource::App\Filament\Resources{closure}(): Argument #1 ($record) must be of type Illuminate\Database\Eloquent\Model, null given, called in /Users/zoba-mac/Sites/laravel/navisafe/vendor/filament/support/src/Concerns/EvaluatesClosures.php on line 35

Consider that, few lines below I've the state() declaration for the same column:

->state(function (Model $record) { return $record->orderSuppliersToBeApprovedByShipyard()->count(); })

...which gives no errors (if I comment the visible() invocation).

Expected behavior

I expect the $record to be correctly evaluated as usual.

Steps to reproduce

1 - create a Filament Resource 2 - add "table" method with any given column 3 - choose any column, call the "visible" method on it, passing a Closure with a Model $record as parameter

Reproduction repository (issue will be closed if this is not valid)

https://github.com/giovanniledda/navisafe

Relevant log output

Illuminate \ Contracts \ Container \ BindingResolutionException PHP 8.2.22 11.8.0 An attempt was made to evaluate a closure for [Filament\Tables\Columns\TextColumn], but [$record] was unresolvable.

Donate 💰 to fund this issue

Fund with Polar

giovanniledda commented 1 month ago

Sorry, duplicate of https://github.com/filamentphp/filament/issues/7757