2016-09-26 34 views
1

我已經添加了一個新的列到我的數據庫中的一個表與遷移。該列已正確創建。但是當我試圖向它寫入數值時,沒有任何東西會被寫入。這是我的代碼:Laravel - 沒有寫任何東西到表中的新列

if ($alternativeId > 0) { 
      $answered = 'yes'; 
     } 
     else { 
      $answered = 'not';  
     } 

     $answer = Answer::create([ 
      'question_id' => $questionId, 
      'player_id' => $player->id, 
      'quiz_id' => $quiz->id, 
      'score' => $score, 
      'answered' => $answered 
     ]); 

變量$回答得值,我已經測試,在控制檯上,當我與測試它:

return ['answered' => $answered] 

而且我在那裏得到正確的價值觀,但沒有寫在DB中。不知道爲什麼會發生這種情況?

+0

您是否已將該列添加到模型中的'$ fillable'屬性? – aynber

回答

5

您需要在$fillable數組中添加answered,因爲create方法使用mass assignment

+1

當然,我錯過了,謝謝! – Marco