【问题标题】:Field 'fullname' doesn't have a default value (SQL: insert into `contacts` (`updated_at`, `created_at`)字段 'fullname' 没有默认值(SQL:插入到 `contacts` (`updated_at`, `created_at`)
【发布时间】:2021-03-30 07:27:25
【问题描述】:

我在 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'));
    }

【问题讨论】:

    标签: php mysql laravel laravel-5.6


    【解决方案1】:

    模型的save 方法不带属性,它保存模型当前的状态。目前您正在保存一个没有属性的模型,但 Eloquent 会自动设置时间戳。您可以在创建新实例时传递要为模型设置的属性:

    $contact = new Contact($validated);
    
    if ($contact->save()) {
    

    您必须确保在模型上设置“可填充”才能像这样填充这些属性。

    【讨论】:

    • 嗨@lagbox,哦,我明白了。非常感谢您的回答。
    猜你喜欢
    • 2020-11-28
    • 1970-01-01
    • 1970-01-01
    • 2021-11-10
    • 2013-04-18
    • 1970-01-01
    • 2014-08-17
    • 2019-01-05
    • 2016-11-04
    相关资源
    最近更新 更多