Ludwig
Ludwig

Reputation: 1791

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?

Upvotes: 1

Views: 131

Answers (1)

Alexey Mezenin
Alexey Mezenin

Reputation: 163758

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

Upvotes: 5

Related Questions