简体   繁体   中英

Laravel 5.4 field doesn't have a default value

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?

删除受guarded数组并添加fillable

protected $fillable = ['user_id', 'deal_id'];

If you would like to revert to previous behavior, update your

config/database.php

file and set 'strict' => false for your connection.

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.

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 .

Alexey Mezenin's Answer is correct and a good one.

Another way i used around it, for those who want to maintain the guarded empty array is to create a new Match object and put in the attributes and save.

            $match->user_id = $id;
            $match->deal_id = $deal->id;
            $match->matched_on = $match->freshTimestamp();
            $match->save();

I am using Laravel 8 and fixed this error thorugh this two steps:

  1. move the word from $guarded array to $fillable array in User Mode

  2. Config.database.php: 'strict' => false in the array of 'mysql'

Another way around this error is to include

'strict' => false,

into config/database.php within mysql array

When manually importing / exporting the databases, check if the transfer of all table settings was successful. If you forget to add an auto increment primary key, Laravel doesn't fill the value for you.

Adding the AUTO_INCREMENT afterwards will solve the problem.

I had this error but my wrong was making class model:

$book = new Book();

While this is true

$book = new Book($request->all());

changing your "config/database.php" won't help. If you're getting this error, you're not sending the data to database correctly. check your function in your controller, the create() method is probably being blocked by an if statement or something. or

if it's an API, check the post request from the frontend that's where your issue is. make sure the form is correctly passed into to request.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM