phplaravellaravel-filamentfilamentphp

Can a select field be dependable of 2 or more fields?


I have followed instructions here https://filamentphp.com/docs/3.x/forms/advanced#dependant-select-options to make a select form dependable from another field in the Edit form. Here is the code of the select field workflow_transition_id dependable from workflow_id TextInput field, which works as expected:

This is in the resource.php

Select::make('workflow_id')
    ->options(Workflow::all()->pluck('name', 'id'))
    ->label('Workflow')
    ->searchable()
    ->preload()
    ->live()
    ->required(),
TextInput::make('workflow_status_id')
    ->label('Current Status')
    ->live()
    ->hiddenOn('create'),
Select::make('workflow_transition_id')
    ->relationship(
        name: 'workflow.workflow_transitions',
        titleAttribute: 'name',
        modifyQueryUsing: fn (Builder $query, Get $get) => $query->where('workflow_id', $get('workflow_id')),
    )
    ->label('New status')
    ->disabled(fn(Get $get) : bool => ! filled($get('workflow_id')))
    ->searchable()
    ->preload()
    ->required(),

Now, I want to make the select field workflow_transition_id dependable from one additional field workflow_status_id. To accomplish this, modified the "modifyQueryUsing" attribute by passing a second Get argument and adding a second **where **clause:

modifyQueryUsing: fn (Builder $query, Get $get, Get $get2) => $query->where('workflow_id', $get('workflow_id'))->where('from_workflow_status_id', $get2('workflow_status_id')),

After doing this, I get this error from Laravel:

Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization

Perhaps I am doing something that I am yet to spot it.

If I try to hardcode $get2('workflow_status_id') by a valid workflow id (i.e. 2), then it works.

I have also tried to use the option attribute (instead of the relationship) and I get the same error:

->options(function(Get $get, Get $get2) { return WorkflowTransition::where('workflow_id', $get('workflow_id'))->where('from_workflow_status_id', $get2('workflow_status_id'))->pluck('name'); })

This is in the edit form which means this workflow_status_id comes from the DB.

I have gone over several Stackoverflow, Google, Laracasts, Filamentphp website and other forums and I have not seen anybody implementing a dependable select with 2 dependent fields.

I am using:


Solution

  • I figured it out. I don't need to pass the Get argument twice in the function. I just need to declare it once and we can use it as many times as required inside the function.

    Select::make('workflow_transition_id')
        ->relationship(
            name: 'workflow.workflow_transitions',
            titleAttribute: 'name',
            modifyQueryUsing: fn (Builder $query, Get $get) => $query->where('workflow_id', $get('workflow_id'))->where('from_workflow_status_id', $get('workflow_status_id')),
        )
        ->label('New status')
        ->disabled(fn(Get $get) : bool => ! filled($get('workflow_id')))
        ->searchable()
        ->preload()
        ->required(),