Jefferson Farid
Jefferson Farid

Reputation: 37

Laravel 9 new column added to an existing table is not saving data

This is my original table called questions:

public function up()
    {
        Schema::create('questions', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('slug');
            $table->string('image')->nullable();
            $table->string('audio')->nullable();
            $table->string('type');
            $table->unsignedBigInteger('evaluation_id');
            $table->foreign('evaluation_id')->references('id')->on('evaluations')->onDelete('cascade');

            $table->timestamps();
        });
    }

And with this code, I added a new column to the existing table:

 php artisan make:migration add_rule_to_questions_table --table=questions
 php artisan migrate

In the migration file of the new column added this in up() method:

public function up()
    {
        Schema::table('questions', function (Blueprint $table) {
            $table->longText('rule')->nullable();
        });
    }

At this point, the new column added succesfully to the database. But, when I try to add data to the new column of the "questions" table, data is not saved in the database.

In create form I use this code:

<div class="form-group">
                <label>Rules:</label>
                <textarea name="rule" id="rule" class="form-control" value="{{old('rule')}}"></textarea>
                
                @error('rule')
                    <small class="text-danger">{{$message}}</small>
                @enderror
   </div>

Finally in store method() of controller I save data with this code:

public function store(Request $request){
    Question::create([
                'title' => $request->title,
                'slug' => $request->slug,
                'evaluation_id' => $request->evaluation_id,
                'type' => "OM",
                'rules' => $request->rule,
                
            ]);
}

But the new column is not saving data, what could be the error?

Upvotes: 1

Views: 397

Answers (1)

Vlad
Vlad

Reputation: 891

You need add rules to array $fillable in your Question model

Upvotes: 2

Related Questions