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

Time:11-14

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 >
                <label>Rules:</label>
                <textarea name="rule" id="rule"  value="{{old('rule')}}"></textarea>
                
                @error('rule')
                    <small >{{$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?

CodePudding user response:

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

  • Related