Support the ongoing development of Laravel.io →
Database Eloquent

I cannot figure out why im not allowed to insert a null or empty while SQL default is set

got this error Integrity constraint violation: 1048 Column 'comment' cannot be null

my migration is

$table->integer('user_id')->unsigned();
            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade')
                ->onUpdate('cascade');
            $table->string('verse');
            $table->text('content');
           $table->string('comment',512)->default('No Tag');

and my controller is


$post = (object) $request->all();
        $favorite = new FavoriteVerse($request->all());
        $favorite->getUser()->associate($this->user);
        $favorite->save();

and my form is

{{csrf_field()}}
                            <input type='hidden' id='modalInputFullReference' name='verse' value=''/>
                            <input type='hidden' id='modalInputContent' name='content' value=''/>
                            <div class='box-body'>
                                <div class='form-group'>
                                    <label>Tag, about this verse</label>
                                    <input class="form-control" placeholder='Ex. a,b,c' name='comment' type="text" />
                                </div>
                            </div>

any thought is appreciated thankyou

Last updated 3 years ago.
0

try this in migration file

$table->string('comment',512)->default('No Tag')->nullable();

0

its value would always be null instead of the default value

Last updated 7 years ago.
0

nullable() allows the attribute to be null, with default() the attribute will take the default value instead of null

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.