更新 `best_answer_id` SQL 不报错
public function it_knows_if_it_is_the_best()
{
// create Answer
$answer = create(Answer::class);
$this->assertFalse($answer->isBest());
$answer->question->update(['best_answer_id' => $answer->id]);
$this->assertTrue($answer->isBest());
}
这里一直返回的是
There was 1 failure:
1) Tests\Unit\AnswerTest::it_knows_if_it_is_the_best
Failed asserting that false is true.
/var/www/laravel/larazhihu/tests/Unit/AnswerTest.php:27
并不会报 SQL 错误,无论 column
存在与否, $answer->question->update(['best_answer_id' => $answer->id]);
一直返回 true
.
查了一下,似乎是因为使用了 $guarded=['id']
导致在不存在的 column
并不会报错,而是不执行语句。反之使用 $fillable=['best_answer_id']
会导致 SQL 错误。
请问下 按教程里的代码走 如何让 update 报 SQL 错误?
推荐文章: