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?
Remove the guarded array and add the fillable instead:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With