Marco Marco - 2 years ago 71
MySQL Question

Laravel - not writing anything to new column in table

I have added a new column to a table in my DB with migration. The column was created correctly. But when I am trying to write values to it, nothing gets written. This is my code:

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

The variable $answered gets a value, I have tested that in the console, when I was testing it with:

return ['answered' => $answered]

And I was getting the correct values there, but nothing was written in the DB. Not sure why is that happening?

Answer Source

You need to add answered in the $fillable array, because create method uses mass assignment.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download