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
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