Laravel 5.4 field doesn't have a default value
Solution 1
Remove the guarded
array and add the fillable
instead:
protected $fillable = ['user_id', 'deal_id'];
Solution 2
If you would like to revert to previous behavior, update your
config/database.php
file and set 'strict' => false
for your connection.
Solution 3
If you have a constructor in your model, just make sure it has a call to a parent constructor as well:
public function __construct( array $attributes = array() ) {
// mandatory
parent::__construct($attributes);
//..
}
Otherwise, it will break some functionality like Model::create
.
Solution 4
Since it was a unique field in my case, I could not make it nullable.
For me, I had an empty constructor which was causing the issue don't know why. Please comment if anyone knows the reason.
public function __construct(){
}
Commenting/removing it resolved the issue.
Solution 5
I am using Laravel 8 and fixed this error thorugh this two steps:
move the word from
$guarded
array to$fillable
array in User ModeConfig.database.php:
'strict' => false
in the array of'mysql'
John David
6+ years as Software, Technology, and Security Architect with significant time in all aspects of the Software Development Life Cycle. I am a great fit for any aspect of Engineering from Requirement Gathering to Implementation Deployment and maintenance, the following are some of the things about me that makes me capable of working with your team Expert at Frontend and Backend Web Development Skills (Vue, React, Node, Laravel) Thorough understanding of Data Structures and algorithms Knowledge of and adherence to Software Development Best Practices Proven Track Record of Proper Documentation for future maintenance Strong Knowledge of Object Oriented programming languages, paradigms, constructs, and idioms. Experience with code versioning using Git. Experience using Agile methodology for development purpose Continuous Integration and Delivery Database Planning, Requirements Gathering, and Analysis Test Driven Development Strong Knowledge REST API best practices Aside from my technical abilities, I have decent experience working with remote and distributed teams, i am a Self-motivated and Structured person, i take initiative and always eager to find ways of improving an existing system. You can also find me on Twitter, LinkedIn, GitHub, and GitLab.
Updated on July 15, 2022Comments
-
John David almost 2 years
I am having this error and none of the googled result i checked is similar to my problem.
I have an application with class Deal, User, and Matches
A deal has many matches. A user has many matches. A user has many deals.
I am attempting to create a new Match using my Deal object
$deal->matches()->create(['user_id'=>$id]);
This is my match class, i have defined all needed relationships
class Match extends Model { /** * The attributes that are mass assignable. * * @var array */ protected $guarded = []; public $timestamps = false; public $expired_on = ""; public static function boot() { parent::boot(); static::creating(function ($model) { $model->matched_on = $model->freshTimestamp(); }); } public function __construct(){ $d = (new \DateTime($this->matched_on))->modify('+1 day'); $this->expired_on = $d->format('Y-m-d H:i:s'); } /** * Get the user that owns the match. */ public function user() { return $this->belongsTo('App\User'); } /** * Get the deal that owns the match. */ public function deal() { return $this->belongsTo('App\Deal'); } }
And i keep getting this error when i attempt to create a new match.
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))I have my guarded to be an empty array, what could be the problem?
-
John David about 7 yearsWill do that, but nothing is wrong with using guarded according to the documentation
-
blamb over 6 yearsDo you need to run any artisan clear commands, or refresh migrate after this?
-
grantDEV over 6 yearsUse new comand in Laravel 5.5 php artisan migrate:fresh --seed and have fun :)
-
blamb over 6 yearsThanks. So, your saying it does need a rerun of the migration then? For the record i think that is
php artisan migrate:refresh --seed
. Unless there is a fresh I don't know about. ;-) -
grantDEV over 6 yearsyes I did that and it worked, migrate:fresh is new command look that laravel-news.com/laravel-5-5
-
Derrick Miller about 5 yearsCan confirm this is a thing. I just ran into it as well. I removed the empty constructor class and the "field doesn't have a default value" error disappeared.
-
Vanlalhriata almost 4 years@vivek-takrani See davee44's answer for the reason. Apparently the fields get passed to the parent through the constructor. Calling the parent constructor with arguments solved the problem for me.
-
Alexander Ivashchenko almost 4 yearsConfirmed. Need to call parent constructor ! and pass attributes: stackoverflow.com/a/59866152/3163536
-
puntable over 3 yearsConfirmed. Empty constructor in class was the problem.
-
Hoytman over 3 yearsThis fixed my problem
-
Urbycoz almost 3 yearsI spent forever on this issue and nothing helped until I tried this.
-
seb_dom over 2 years'guarded' does the opposite (like $guarded = ['id']; means do not allow to update the id), so, you should either use 'fillable' or 'guarded'.