如何修復 Laravel 5.4 中的「欄位沒有預設值」錯誤?

Linda Hamilton
發布: 2024-11-13 16:23:02
原創
134 人瀏覽過

How to Fix the

Resolving "Field Doesn't Have a Default Value" Error in Laravel 5.4

When attempting to create a new Match entity using the Deal model, you may encounter the error "Field 'user_id' doesn't have a default value." This occurs despite having defined an empty guarded array in the Match class.

To resolve this issue, you should replace the guarded array with the fillable array:

protected $guarded = [];
protected $fillable = ['user_id', 'deal_id'];
登入後複製

By specifying the fillable array, you explicitly define the fields that can be mass assigned to the model. This ensures that only authorized fields are updated when creating or updating the Match entity.

The fillable array should include all the fields that you want to be able to set when creating or updating the model using the mass assignment syntax. In this case, you want to be able to set both the user_id and deal_id when creating a new Match.

Once you have made this change, you should be able to create new Match entities without receiving the "Field Doesn't Have a Default Value" error.

以上是如何修復 Laravel 5.4 中的「欄位沒有預設值」錯誤?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板