字段 'fullname' 没有默认值(SQL:插入到 `contacts`(`updated_at`、`created_at`)
Field 'fullname' doesn't have a default value (SQL: insert into `contacts` (`updated_at`, `created_at`)
我在 Laravel 5.6 上收到以下错误。但是我发送的值不为空。我在本地 Docker 运行 MySQL。当我销毁容器并创建一个新的 mysql 容器时,它可以正常工作一段时间。我认为这是 RAM 错误。
那是我的 macbook:
MacBook Pro (Retina, 13-inch, Late 2013) Mojave 10.14.6
Processor: 2.4 GHz Intel Core i5
Ram: 8 GB 1600 MHz DDR3
Graphics: Intel Iris 1536 MB
您有什么意见或建议吗?或者解决方案?
错误:
SQLSTATE[HY000]: General error: 1364 Field 'fullname' doesn't have a default value (SQL: insert into `contacts` (`updated_at`, `created_at`) values (2020-12-20 06:00:49, 2020-12-20 06:00:49))
Previous exceptions
SQLSTATE[HY000]: General error: 1364 Field 'fullname' doesn't have a default value (HY000)
SQLSTATE[HY000]: General error: 1364 Field 'fullname' doesn't have a default value (HY000)
ContactController.php:
public function store(Request $request)
{
$validated = $request->validate([
'fullname' => 'required',
'email' => 'required|email',
'phone' => 'required',
'message' => 'required|between:10,1000',
// 'captcha' => 'required|captcha',
]);
$contact = new Contact;
if($contact->save($validated)) {
session()->flash('success', trans('Kayıt başarıyla eklendi'));
} else {
session()->flash('success', trans('Kayıt eklenirken bir sorun oluştu'));
}
return redirect(route('contact.create'));
}
模型的 save
方法不接受属性,它保存模型当前的状态。目前您正在保存没有属性的模型,但 Eloquent 会自动设置时间戳。您可以在创建新实例时传递要为模型设置的属性:
$contact = new Contact($validated);
if ($contact->save()) {
您必须确保在模型上设置“可填充”才能像这样填充这些属性。
我在 Laravel 5.6 上收到以下错误。但是我发送的值不为空。我在本地 Docker 运行 MySQL。当我销毁容器并创建一个新的 mysql 容器时,它可以正常工作一段时间。我认为这是 RAM 错误。
那是我的 macbook:
MacBook Pro (Retina, 13-inch, Late 2013) Mojave 10.14.6
Processor: 2.4 GHz Intel Core i5
Ram: 8 GB 1600 MHz DDR3
Graphics: Intel Iris 1536 MB
您有什么意见或建议吗?或者解决方案?
错误:
SQLSTATE[HY000]: General error: 1364 Field 'fullname' doesn't have a default value (SQL: insert into `contacts` (`updated_at`, `created_at`) values (2020-12-20 06:00:49, 2020-12-20 06:00:49))
Previous exceptions
SQLSTATE[HY000]: General error: 1364 Field 'fullname' doesn't have a default value (HY000)
SQLSTATE[HY000]: General error: 1364 Field 'fullname' doesn't have a default value (HY000)
ContactController.php:
public function store(Request $request)
{
$validated = $request->validate([
'fullname' => 'required',
'email' => 'required|email',
'phone' => 'required',
'message' => 'required|between:10,1000',
// 'captcha' => 'required|captcha',
]);
$contact = new Contact;
if($contact->save($validated)) {
session()->flash('success', trans('Kayıt başarıyla eklendi'));
} else {
session()->flash('success', trans('Kayıt eklenirken bir sorun oluştu'));
}
return redirect(route('contact.create'));
}
模型的 save
方法不接受属性,它保存模型当前的状态。目前您正在保存没有属性的模型,但 Eloquent 会自动设置时间戳。您可以在创建新实例时传递要为模型设置的属性:
$contact = new Contact($validated);
if ($contact->save()) {
您必须确保在模型上设置“可填充”才能像这样填充这些属性。