Table must not be accessed before initialization

Hello, I'm encountering an issue with the dynamic visible function in my code. Whenever I select a value on input_1, I receive the following error: Typed property App\Livewire\TagsManagerComponent::$table must not be accessed before initialization. Here is an example of code to reproduce the problem: TagsManagerComponent.php :
class TagsManagerComponent extends Component implements HasForms, HasTable
{
use InteractsWithForms;
use InteractsWithTable;

public function mount(): void
{
$this->form->fill();
}

public function makeForm(): Form
{
return Form::make($this)->schema([
TextInput::make('name')->required()->maxLength(255),
ColorPicker::make('color')->required(),
Select::make('input_1')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
->live()
->required(),
TextInput::make('input_2')
->label('2')
->maxLength(255)
->visible(function (Get $get) {
$input = $get('input_1');
return $input !== null && $input !== 'draft' && $input !== 'reviewing';
}),
]);
}

public function table(Table $table): Table
{
return $table
->query(Tag::query())
->columns([
TextColumn::make('name')->searchable()->sortable(),
ColorColumn::make('color')->searchable()->sortable(),
TextColumn::make('input_1')->searchable()->sortable(),
TextColumn::make('input_2')->searchable()->sortable(),
])
->headerActions([
Action::make('create')->label('Add Tag')->form($this->makeForm()->getComponents()),
])
->paginated([10, 25, 50, 100, 'all']);
}

public function render()
{
return view('livewire.tags-manager-component');
}
}
class TagsManagerComponent extends Component implements HasForms, HasTable
{
use InteractsWithForms;
use InteractsWithTable;

public function mount(): void
{
$this->form->fill();
}

public function makeForm(): Form
{
return Form::make($this)->schema([
TextInput::make('name')->required()->maxLength(255),
ColorPicker::make('color')->required(),
Select::make('input_1')
->options([
'draft' => 'Draft',
'reviewing' => 'Reviewing',
'published' => 'Published',
])
->live()
->required(),
TextInput::make('input_2')
->label('2')
->maxLength(255)
->visible(function (Get $get) {
$input = $get('input_1');
return $input !== null && $input !== 'draft' && $input !== 'reviewing';
}),
]);
}

public function table(Table $table): Table
{
return $table
->query(Tag::query())
->columns([
TextColumn::make('name')->searchable()->sortable(),
ColorColumn::make('color')->searchable()->sortable(),
TextColumn::make('input_1')->searchable()->sortable(),
TextColumn::make('input_2')->searchable()->sortable(),
])
->headerActions([
Action::make('create')->label('Add Tag')->form($this->makeForm()->getComponents()),
])
->paginated([10, 25, 50, 100, 'all']);
}

public function render()
{
return view('livewire.tags-manager-component');
}
}
View :
{{ $this->table }}
{{ $this->table }}
No description
Solution:
likely has to do with $this->makeForm()->getComponents() which you call when you make your action I'd refactor your makeForm() method to just return an array of components ```php private static function getFormComponents(?params): array...
Jump to solution
3 Replies
Solution
Jordy
Jordy4w ago
likely has to do with $this->makeForm()->getComponents() which you call when you make your action I'd refactor your makeForm() method to just return an array of components
private static function getFormComponents(?params): array
{
return [
TextInput::make(),
...
];
}
private static function getFormComponents(?params): array
{
return [
TextInput::make(),
...
];
}
Jordy
Jordy4w ago
you dont really need $this for your form,nor do you need the entire form for your action, just the schema
Pierrot
PierrotOP4w ago
Thank you ,it fixed the problem !

Did you find this page helpful?