0 php laravel laravel-filament
模型 Employee 与模型 Detail 相关,其中包含每个员工的地址和电话号码。员工资源中有一个表单,其中包含员工和详细信息表的字段。如何从该表单将数据保存到详细模型?
在员工资源的表单函数中,我尝试使用点表示法,但数据未保存。
Card::make()
->schema([
TextInput::make('detail.email')
->email(),
TextInput::make('detail.mobile'),
])->columns(2)
Run Code Online (Sandbox Code Playgroud)
小智 5
您只需要添加另一个步骤。假设您已经detail在模型中定义了关系Employee,请转到您的CreateEmployee页面并添加此覆盖方法,handleRecordCreation如下所示:-
namespace App\Filament\Resources\EmployeeResource\Pages;
use App\Filament\Resources\EmployeeResource;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Database\Eloquent\Model;
class CreateEmployee extends CreateRecord
{
protected static string $resource = EmployeeResource::class;
protected function handleRecordCreation(array $data): Model
{
$record = static::getModel()::create($data);
$record->detail()->create($data['detail']);
return $record;
}
}
Run Code Online (Sandbox Code Playgroud)