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
New contributor
mehmet Dora is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.