0

I started new project on Laravel and wanted to add 2 extra columns to User table (phone_number, position). But when I try to register I have this error:

SQLSTATE[HY000]: General error: 1364 Field 'phone_number' doesn't have a default value (SQL: insert into users (name, email, password, updated_at, created_at) values (Lidija, lidija@gmail.com, $2y$10$/BQju2korC4YOm.6yCb2i.sOoQQJTgzEyklrHPfJPF1XQwx86Vyky, 2020-10-09 15:35:36, 2020-10-09 15:35:36))

I did migrate tables. Maybe there is something wrong with input values in blade.php?

User model:

My User migration:

UserController:

Blade.php:

I did php artisan optimize, didn't help. Also, I tried dd($request->all()) in the Store method to see what's there, but it showed nothing, just this error 1364 again.

What could be the problem?

Akash Kumar Verma
  • 3,185
  • 2
  • 16
  • 32
  • 1
    So you also have a column called `phone_number` in that table. BUT you are not setting a value for that column!! So either set a phone number OR make the column in the database allowed to be blank – RiggsFolly Oct 09 '20 at 15:52
  • Add: "blank" means nullable in this case. – Flo Espen Oct 09 '20 at 15:56
  • Set Default NULL otherwise pass phone number in request. – Bhargav Variya Oct 09 '20 at 15:58
  • @RiggsFolly but I have inputs in my view.. – Lydia Syber Oct 09 '20 at 15:58
  • @BhargavVariya I know I can make it default in migration, but I want to pass the value through my view. I showed inputs in screenshot. something wrong with my inputs? – Lydia Syber Oct 09 '20 at 16:00
  • @RonvanderHeijden I use composer require laravel/jetstream, php artisan jetstream:install livewire. It is something new, 8 months earlier when I used Laravel, there was not such thing – Lydia Syber Oct 09 '20 at 16:23
  • In future, please ensure you add code and error messages to your question. Pictures are not particularly helpful. Thanks! – miken32 Oct 09 '20 at 16:43
  • Note that `route("register")` probably does not go through `UserController@store`. Check output of `artisan route:list` to see what controller and method handle this route. – miken32 Oct 09 '20 at 16:51

1 Answers1

0

You have to either set a default value for phone_number (like null for example), or pass it with the arguments, otherwise, your requests will always fail.

Change your migration to something like this in order to make phone_number null if not passed as an argument.

$table->integer('phone_number')->unique()->nullable();

Hamza Mogni
  • 672
  • 1
  • 7
  • 21
  • Yes, I can do nullable, but this way I have no value. I pass arguments through the form, but it seems like it does not work, because in both ways I do not get argument from the form – Lydia Syber Oct 09 '20 at 16:19
  • for debugging, try returning the whole request and see if the server does even get the phone_number with the request, if not, there is likely a problem with your blade template. – Hamza Mogni Oct 09 '20 at 16:26