· filament · 1 min read

Fill a modal form with the value from ActiveTab in FilamentPHP

Quality of life for the users of your panel!

A quick quality-of-life for your users to be more productive :)

Assume that your users need to create translations for a word. These translations are displayed as shown in the image below, utilizing a tab to showcase all available languages and a table underneath with the translations.

Prefilled field

  1. Click on “English” tab
  2. Add a new word
  3. The Available Language is prefilled.

Here is the code to achieve this:

  1. Generating Tabs from the Database
public function getTabs(): array
{
    $tabs = [
        'all' => Tab::make(),
    ];
    $languages = AvailableLanguage::all();
    foreach ($languages as $language) {
        $tabs[$language->name] = Tab::make()
            ->modifyQueryUsing(fn (Builder $query) => $query->where('available_language_id', '=', $language->id));
    }
    return $tabs;
}
  1. Configuring the Select Field
Select::make('available_language_id')
  ->default(function () {
    if ( $this->activeTab != 'all') {
        $availableLanguage = AvailableLanguage::where('name', $this->activeTab)->first();
        return $availableLanguage?->id;
    }
    return null;
  })

You could return a default value instead of null in case the user selects “all”. There’s no need to check if it’s in Edit or Create mode via $operation since the default is called only in “create mode”.

Back to Blog