Solution when column value becomes NULL even if create is executed in Laravel
Problem
- -I ran
createwithLaravel, but the value inserted wasNULLeven though I specified the value. - -When executing
createwithLaravel, **SQLSTATE [HY000]: General error: 1364 Field (column name) doesn't have a default value** occurred.
solution
** Review the model **
In my case, I didn’t fill in the corresponding column for $ fillable in the model.
class Model extends Authenticatable
{
use Notifiable;
/ **
* The attributes that are mass assignable.
*
* @var array
* /
protected $ fillable = [
'name','email','password',
// (Specify the column that was NULL even if create was executed here)
];;
Cause
In Laravel, when saving records etc. in the database using create etc. through the model, a malicious user creates columns etc. without permission and makes an administrator so that that person does not become a virtual administrator A function to protect is provided as standard.
Therefore, if you do not specify the column to be created and inserted with create with $ fillable etc., you will think that Laravel may be a malicious user, and automatically insert NULL to protect it. It was the cause.
** Don’t forget to protect columns that do not have default values such as create with $ fillable !! **