Anonymous Chatbox
Anonymous Chatbox

Reputation: 481

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL Even after making required fields null

I have made all the required fields nullable but still I am getting this error

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: public_problems.name (SQL: insert into "public_problems" ("answer", "helper_id", "helper_name", "updated_at", "created_at") values (Hey Bro Everythings Gonna be Alright, 2, kakashi, 2020-07-30 07:35:05, 2020-07-30 07:35:05))

Here's my Database:-

public function up()
    {
        Schema::create('public_problems', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->integer('user_id')->unsigned();
            $table->text('answer')->nullable();
            $table->integer('helper_id')->unsigned()->nullable();
            $table->string('helper_name')->nullable();
            $table->string('title');
            $table->text('problem');
            $table->string('filled_by');

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

Model:-

class PublicProblem extends Model
{
    protected $fillable = ['name', 'user_id', 'title', 'problem', 'filled_by', 'helper_id', 'answer', 'helper_name'];

}

I have divided this field into 2 functions and they are:-
Controller

public function store(Request $request)
    {
        $data = request()->validate([
        'title' => 'required',
        'problem' => 'required',

      ]);
         PublicProblem::create([
            'title'=>$data['title'],
            'problem'=>$data['problem'],
            'name'=>Auth::user()->name,
            'user_id'=>Auth::user()->id,
            'filled_by'=>Auth::user()->uuid,

          ]);
         return redirect('/home');
    }

public function answer(Request $request) //The Error Occurs when I try to execute this function 
    {
    

        $data = request()->validate([
            'answer' => 'required',
        ]);

        PublicProblem::create([
            'answer' => $data['answer'],
            'helper_id' => Auth::user()->id,
            'helper_name' => Auth::user()->name,
        ]);
        return redirect('/home');
    }

The error occurs when I try to execute answer() function
Cant figure out what's causing it please help

Upvotes: 1

Views: 827

Answers (2)

STA
STA

Reputation: 34678

Error here, your name, title, user_id, problem, filled_by fields are required, make this filled nullable on your migration or you should give a value while insert into table, otherwise SQL throw an error :

PublicProblem::create([
            'name' => "Name", // Not null
            'title' => "Title"; // Not null
            'problem' => "Problem", // Not null
            'filled_by' => "Filled by", // not null
            'user_id' => "1", // not null
            'answer' => $data['answer'],
            'helper_id' => Auth::user()->id,
            'helper_name' => Auth::user()->name,
        ]);

Upvotes: 2

Aless55
Aless55

Reputation: 2709

Problem:

SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: public_problems.name

Solution:

The name file in your database is not nullable. If you want to call the create method, you will either have to add 'name' => 'someName' or you add the ->nullable() constrained to the name field.

PublicProblem::create([
            'answer' => $data['answer'],
            'helper_id' => Auth::user()->id,
            'helper_name' => Auth::user()->name,
            'name' => 'someName',
        ]);

Please Note:

You have some more fields which are not nullable, so the error might happen again for another field. (i commented it below)

$table->id();
$table->string('name'); //always needs a value
$table->integer('user_id')->unsigned(); //always needs a value
$table->text('answer')->nullable();
$table->integer('helper_id')->unsigned()->nullable();
$table->string('helper_name')->nullable();
$table->string('title'); //always need a value
$table->text('problem'); //always needs a value
$table->string('filled_by'); //always needs a value

Upvotes: 3

Related Questions