在 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)
设定任何其他栏位的值。
实现效果:
