John David
John David

Reputation: 772

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?

Upvotes: 40

Views: 127859

Answers (10)

Real-Artisan
Real-Artisan

Reputation: 21

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.

Upvotes: 0

ParisaN
ParisaN

Reputation: 2092

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

$book = new Book();

While this is true

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

Upvotes: 0

mhellmeier
mhellmeier

Reputation: 2281

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.

Upvotes: 1

W Kenny
W Kenny

Reputation: 2069

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'

Upvotes: 7

Oyekan Oluwatobi
Oyekan Oluwatobi

Reputation: 97

Another way around this error is to include

'strict' => false,

into config/database.php within mysql array

Upvotes: 5

davee44
davee44

Reputation: 375

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.

Upvotes: 9

vivek takrani
vivek takrani

Reputation: 4010

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.

Upvotes: 9

grantDEV
grantDEV

Reputation: 582

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

config/database.php

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

Upvotes: 40

Alexey Mezenin
Alexey Mezenin

Reputation: 163768

Remove the guarded array and add the fillable instead:

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

Upvotes: 103

John David
John David

Reputation: 772

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();

Upvotes: 5

Related Questions