Field 'fullname' doesn't have a default value (SQL: insert into `contacts` (`updated_at`, `created_at`)

301 views Asked by At

I get the following error on Laravel 5.6. But the values I send are not null. I'm running MySQL in Docker on my local. When I destroy the container and create a new mysql container it works fine for a while. I think this is a RAM error.

that is my 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

Do you have any advice or suggestions? Or a solution?

Error:

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

There are 1 answers

1
lagbox On BEST ANSWER

The Model's save method doesn't take attributes, it saves the model how it currently is. Currently you are saving a model without attributes but Eloquent automatically sets the timestamps. You can pass the attributes you want to set for the model when you create the new instance:

$contact = new Contact($validated);

if ($contact->save()) {

You will have to make sure you set "fillable" on the Model to be able to fill these attributes like this.