Home > Database > Mysql Tutorial > Why am I getting the error 'Field 'user_id' doesn't have a default value' when creating a new match in Laravel 5.4?

Why am I getting the error 'Field 'user_id' doesn't have a default value' when creating a new match in Laravel 5.4?

Mary-Kate Olsen
Release: 2024-11-07 19:54:03
Original
487 people have browsed it

Why am I getting the error

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))
Copy after login

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'];
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template