Я создал красноречивую связь «один к одному» между двумя моделями TokenMatch
модель и Match
модель, но когда я попытался связать TokenMatch с Match, у меня появляется ошибка:
"Field 'tokenmatch_id' doesn't have a default value (SQL: insert into `matches` (`id`, `updated_at`, `created_at`) values (, 2019-04-22 08:55:51, 2019-04-22 08:55:51))
"
Мой код выпуска:
$match = new Match();
$tokenmatch = TokenMatch::find(1);
$match->token()->associate($tokenmatch)->save();
класс соответствия
class Match extends Model
{
public function token()
{
return $this->belongsTo('App\TokenMatch' , 'id', 'tokenmatch_id');
}
}
класс TokenMatch
class TokenMatch extends Model
{
protected $table = 'tokensmatch';
public function match()
{
return $this->hasOne('App\Match','tokenmatch_id');
}
}
соответствия Таблица
Schema::create('matches', function (Blueprint $table) {
$table->bigIncrements('id');
$table->boolean('isFinished')->nullable();
$table->boolean('isWon')->nullable();
$table->unsignedBigInteger('tokenmatch_id');
$table->foreign('tokenmatch_id')->references('id')->on('tokensmatch');
$table->timestamp('created_at')->default(\Illuminate\Support\Facades\DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(\Illuminate\Support\Facades\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));
});
таблица токенов
Schema::create('tokensmatch', function (Blueprint $table) {
$table->bigIncrements('id');
$table->boolean('isUsed')->default(false);
$table->string('token', 15)->unique();
$table->dateTimeTz('expiryDate')->nullable();
$table->boolean('isValid')->default(true);
});
Так что я ожидал, что в поле "tokenmatch_id" таблицы Matches не будет нулевого значения при сохранении совпадения новой модели
...
$match->token()->associate($tokenmatch)->save();
...