Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading