Home > OS >  Laravel insert default value instead of data sent via Request
Laravel insert default value instead of data sent via Request

Time:10-17

I'm facing a very weird issue on my Laravel app that when I insert data to the database, it always insert the default value instead of the data sent via post request. I dd(die dump) the value before insert into the database and the correct data showing. But on database it always insert the default value in migration file.

Same code works fine for another modal.

my html from code

<div class="form-row">                        
                        <div class="col-md-6 mb-3">
                          <label for="is_free">Is Free</label>
                          <select class="form-control" name="is_free" required>
                                <option value="1">Free</option>
                                <option value="0">Pro Only</option>
                            </select>
                            @error('is_free')
                            <div class="text-danger mt-2 small ">{{ $message }}</div>
                            @enderror
                        </div>
                        </div>

migration file

public function up()
    {
        Schema::create('chapters', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->string('slug');
            $table->string('description');
            $table->mediumText('content');
            $table->tinyInteger('is_free')->default(1);
            $table->timestamps();
        });
    }

controller

Chapter::create([

            'title' => $request->title,
            'slug' => $request->slug,
            'description' => $request->description,
            'content' => $request->content,
            'is_free' => $request->is_free,

        ]);

CodePudding user response:

in your Chapter Model, make sure you have $fillable property with correct properties:

class Chapter extends Model
{
 protected $fillable = ['title','slug','description','content','is_free'];
....
}
  • Related