skip to Main Content

SQLSTATE[HY000]: General error: 1364 Field ‘blog_id’ doesn’t have a default value (Connection: mysql, SQL: insert into comments (content, status, blogun_id, profilin_id, updated_at, created_at) values (vsvd, 1, 2, 2, 2024-07-20 23:36:02, 2024-07-20 23:36:02))

I try all solution ways I know but I can’t fix this
This is my function to save data;

$validatedData = Validator::make($request->all(), [
             'comment' => 'required',
             'blog_id' => 'required',
        ]);

        if ($validatedData->fails()) {
            // Hata mesajlarını alın
            $errors = $validatedData->errors()->all(); // istersek laravelin kendi hatam mesajını da gönderebiliriz

            // E-posta veya şifrenin hatalı olduğunu belirten özel bir hata mesajı oluşturun
            $errorMessage = 'Girilen email veya şifre formatı hatalı!';

            // Hata mesajlarını kullanıcıya gösterin
            return redirect()->back()->with('error',$errors);
        }


            $comment = NEW Comment();
            $comment->content = $request->comment;
            $comment->status = true;
            $comment->blogun_id = (int)$request->blog_id;
            $comment->profilin_id = Auth::user()->profile->id;

            $comment->save();

And this is my comments migration;

Schema::create('comments', function (Blueprint $table) {
            $table->id();

            $table->string('content',250)->nullable(false);
            $table->boolean('status')->default(true)->comment('1 ise aktif, 0 ise kapalı');
            $table->bigInteger('blogun_id')->unsigned()->nullable(false);
            $table->bigInteger('profilin_id')->unsigned()->nullable(false);

            // bir blog veya profil birçok comment e sahip olabilir
            $table->foreignId('blog_id')->references('id')->on('blogs')->cascadeOnDelete();
            $table->foreignId('profile_id')->references('id')->on('profiles')->cascadeOnDelete();

            $table->timestamps();
        });

Error is in save() line , please help me

2

Answers


  1. I think you misunderstand how to use the $table->foreignId(). What you want to do is to add a foreign key constraints in the blogun_id column, right ?

    There are 2 ways to add foreign key constraints in a column the first one is using the foreignId() and the other is using foreign().

    Using the foreign()

    $table->unsignedBigInteger('blogun_id'); // define the column
    $table->foreign('blogun_id')->references('id')->on('blogs')->cascadeOnDelete(); // add the foreign key constraints
    

    Using the foreignId()

    $table->foreignId('blogun_id')->constrained('blogs')->cascadeOnDelete(); // Its the shortcut of foreignId()
    
    $table->foreignId('blogun_id')->references('id')->on('blogs')->cascadeOnDelete();
    

    So the difference between the two is the foreign one you can only use it in the already defined column if it is not defined it will produce an error while foreignId will work without defining a column first since it will automatically create a column for it.

    In the snippet that you posted you are using foreignId() for creating the blog_id column. It means that you are creating the blog_id column with a foreign key constraints so the columns in your table are id,content,status,blogun_id,profilin_id,blog_id, and profile_id. Then when you are saving a data the only columns that are adding a data is content,status,blogun_id,profilin_id which makes the blog_id and profile_id null which produce a error since the 2 columns are not nullable

    Login or Signup to reply.
  2. In your migration file, you created 4 ids

    blogun_id
    profilin_id
    blog_id
    profile_id
    

    However in your save code, you saved both blogun_id and profilin_id but not blog_id and profile_id. Make sure that you added it in your code:

    $comment = NEW Comment();
    $comment->content = $request->comment;
    $comment->status = true;
    $comment->blogun_id = (int)$request->blog_id;
    $comment->profilin_id = Auth::user()->profile->id;
    
    $comment->blog_id = /* whatever the blog id is */
    $comment->profile_id = /* whatever the profile id is */
    
    $comment->save();
    

    Don’t forget to include those ids in the fillable if your Comments model

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search