Laravel Filament 根据 select 选定项改变 input 的值的方法,Laravel Filament 根据 select 选定项自动填充 input。

在 form() 相应字段中添加下面代码: ->schema( FormsComponent

在 form() 相应字段中添加下面代码:

->schema([
    FormsComponentsSelect::make('product_id')
        ->relationship('product', 'name')
        ->live()
        ->afterStateUpdated(function (Get $get, Set $set) {
            $set('price', Product::find($get('product_id'))->price);
        })
        ->required(),
    FormsComponentsTextInput::make('price')
        ->required()
        ->numeric()
        ->prefix('$'),

首先设置select字段的live()方法,然后在afterStateUpdated()方法中,通过$get(fieldName)中取得select字段当前的值,然后用$set(fieldName, newValue)设定任何其他栏位的值。

实现效果: