Error: "Field 'user_id' doesn't have a default value" in Laravel 5.4
Question:
When attempting to create a new match using a Deal object, the following error occurs:
QueryException in Connection.php line 647: SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into matches (deal_id) values (1))
Despite having an empty $guarded array, why is this error being raised?
Answer:
The $guarded array prevents mass assignment to all fields except those explicitly mentioned. To resolve the issue, remove the $guarded array and define the $fillable array instead.
Solution:
In the Match class, add the following:
protected $fillable = ['user_id', 'deal_id'];
This will allow mass assignment to the user_id and deal_id fields, which are necessary for creating a new match.
The above is the detailed content of Why am I getting the error 'Field 'user_id' doesn't have a default value' when creating a new match in Laravel 5.4?. For more information, please follow other related articles on the PHP Chinese website!