mutateFormDataBeforeCreate does not add the field to the query

Hello, I am currently trying to add the user_id to the form array when a user creates a new entry. In the Create class of the Filament recourse I have this:
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['user_id'] = auth()->user()->id;
return $data;
}
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['user_id'] = auth()->user()->id;
return $data;
}
For some reason, the query is this: SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value
INSERT INTO
`websites` (`name`, `url`, `type`, `updated_at`, `created_at`)
VALUES
(
test,
test.nl,
0,
2024 -02 -16 19: 41: 11,
2024 -02 -16 19: 41: 11
)
INSERT INTO
`websites` (`name`, `url`, `type`, `updated_at`, `created_at`)
VALUES
(
test,
test.nl,
0,
2024 -02 -16 19: 41: 11,
2024 -02 -16 19: 41: 11
)
What am I doing wrong?
3 Replies
Luukd_2000
Luukd_20004mo ago
fixed it. I forgot to add user_id to the fillable array.
Douglas Silvestrini
I recommend you to put Model::unguard() inside the boot() method at AppServiceProvider. Never more you'll have that headache
krekas
krekas4mo ago
better not