php 在Filament资源中使用getTableFilterState

ilmyapht  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(146)

说明

在此代码中,在BadgeColumn中,我使用$this->getTableFilterState('created_at')['date']来获取筛选器数据,但这显示错误

不在对象上下文中时使用$this

这是我的PayRollResource.php

public static function table(Table $table): Table
    {
        $date = Carbon::now()->format('Y-m-d');
        return $table
            ->query(Employee::active()->whereDate('created_at', '<=', $date))
            ->columns([
                TextColumn::make('name')
                    ->searchable(),
                TextColumn::make('email')
                    ->searchable(),
                TextColumn::make('phone')
                    ->searchable(),
                TextColumn::make('unique_id')
                    ->searchable(),
                TextColumn::make('salary'),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                BadgeColumn::make('payroll_status')
                    ->label("PayRoll Status")
                    ->colors([
                        'success' => static fn ($state): bool => $state == 'Paid',
                        'danger' => static fn ($state): bool => $state == 'Unpaid',
                    ])
                    ->getStateUsing(function (Employee $record) {
                        return $record->payrollStatus($this->getTableFilterState('created_at')['date']);
              
                    }),
                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Filter::make('created_at')
                    ->form([
                        DatePicker::make('date'),
                    ])

                    ->indicateUsing(function (array $data): array {
                        $indicators = [];
                        if ($data['date']) {
                            $indicators['date'] = 'Date ' . Carbon::parse($data['date'])->toFormattedDateString();
                        }
                        return $indicators;
                    }),

            ])

这是我的工资单页面

<?php

namespace App\Filament\Pages;

use Carbon\Carbon;
use Pages\ViewEmployee;
use App\Models\Employee;
use BezhanSalleh\FilamentShield\Traits\HasPageShield;
use Filament\Pages\Page;
use Filament\Tables\Table;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Contracts\HasTable;
use Filament\Forms\Components\DatePicker;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Actions\Action as ActionsAction;
use Filament\Tables\Columns\BadgeColumn;

class Payroll extends Page implements HasTable
{
    use InteractsWithTable, HasPageShield;
    protected static ?string $navigationIcon = 'heroicon-o-banknotes';

    protected static string $view = 'filament.pages.payroll';

    public function table(Table $table): Table
    {
        $date = Carbon::now()->format('Y-m-d');

        return $table
            ->query(Employee::active()->whereDate('created_at', '<=', $date))
            ->columns([
                TextColumn::make('name')
                    ->searchable(),
                TextColumn::make('email')
                    ->searchable(),
                TextColumn::make('phone')
                    ->searchable(),
                TextColumn::make('unique_id')
                    ->searchable(),
                TextColumn::make('salary'),
                TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                BadgeColumn::make('payroll_status')
                    ->label("PayRoll Status")
                    ->colors([
                        'success' => static fn ($state): bool => $state == 'Paid',
                        'danger' => static fn ($state): bool => $state == 'Unpaid',
                    ])
                    ->getStateUsing(function (Employee $record) {
                        return $record->payrollStatus($this->getTableFilterState('created_at')['date']);
                    }),

                TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
            ])
            ->filters([
                Filter::make('created_at')
                    ->form([
                        DatePicker::make('date'),
                    ])

                    ->indicateUsing(function (array $data): array {
                        $indicators = [];
                        if ($data['date']) {
                            $indicators['date'] = 'Date ' . Carbon::parse($data['date'])->toFormattedDateString();
                        }
                        return $indicators;
                    }),
            ])
            ->actions([
                ActionsAction::make('View')
                    ->color('success')
                    ->icon('heroicon-o-banknotes')
                    ->url(function (Employee $record): string {
                        return route('payroll.view', [$record->id, 'date' => $this->getTableFilterState('created_at')['date']]);
                    })
            ])
            ->bulkActions([]);
    }
}

在本页中,$this->getTableFilterState工作正常。我想改变这个自定义页面到灯丝资源,但在默认的资源页$this->getTableFilterState是不工作!为什么会这样?对此有何建议,请参考
我想在Filament资源页面上使用$this->getTableFilterState

4bbkushb

4bbkushb1#

您遇到的问题是因为$this->getTableFilterState在默认的Filament资源页面中不可用。您似乎有一个自定义Filament页面正在使用此方法,但当您使用默认资源页面时,它无法工作,因为该方法在该上下文中不可用。
如果您想在Filament资源页面中使用$this->getTableFilterState或类似功能,则需要创建自定义资源页面。下面是一个基本示例,说明如何创建具有所需功能的自定义资源页面:

php artisan filament:resource PageName

use Filament\Resource;

public static $icon = 'hereoicon-o-softes';

public $table = \App\Filament\Tables\YourCustomTable::class;

public function table($table)
{
    // Customize your table here
    return $table;
}

}

use Filament\Tables\Table;

使用Filament\Tables\Concerns\InteractsWithTable;
类YourCustomTable扩展Table { use InteractsWithTable;

public function columns()
{
    // Define your table columns
}

public function filters()
{
    // Define your table filters
}

public function actions()
{
    // Define your table actions
}

// Define your custom methods and logic, including the use of $this->getTableFilterState

}

相关问题