FilamentF
Filament3y ago
Flo

Widget Table "Cannot use "::class" on null" error

Hello, I am trying to add a Table to a page that extends from "ManageRelatedRecords".

So in this one I included the widget on "class DatabaseServer extends ManageRelatedRecords":
protected function getHeaderWidgets(): array
{
    return[
        ServerResource\Widgets\CreateDatabaseUserWidget::make([
            'server' => $this->getRecord(),
        ]),
    ];
}


And in my ServerResource page I declared the widget:
public static function getWidgets(): array
{
    return[
        ServerResource\Widgets\CreateDatabaseUserWidget::class,
    ];
}


And here is my Widget page:
<?php

namespace App\Filament\Resources\ServerResource\Widgets;

use App\Models\Server;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Filament\Widgets\TableWidget as BaseWidget;
use Illuminate\Database\Eloquent\Builder;

class CreateDatabaseUserWidget extends BaseWidget
{
    public Server $server;

    public function table(Table $table): Table
    {
        return $table
            ->query(fn (Builder $query) => $query
                ->where('server_id', $this->server->id)
            )
            ->columns([
                TextColumn::make('id'),
            ]);
    }
}


Here is the trace:
[2023-12-19 14:31:58] local.ERROR: Cannot use "::class" on null {"userId":"*******","exception":"[object] (TypeError (code: 0): Cannot use \"::class\" on null at path/to/directory/vendor/filament/tables/src/Table/Concerns/HasRecords.php:76)
[stacktrace]
Solution
Ok, it was just my query that was not correct, here is what works:

class CreateDatabaseUserWidget extends BaseWidget
{
    public Server $server;

    public function table(Table $table): Table
    {
        return $table
            ->query(DatabaseUser::query()->where('server_id', $this->server->id))
            ->columns([
                TextColumn::make('id'),
            ]);
    }
}
Was this page helpful?