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
I think you misunderstand how to use the
$table->foreignId()
. What you want to do is to add a foreign key constraints in theblogun_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 usingforeign()
.Using the
foreign()
Using the
foreignId()
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 whileforeignId
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 theblog_id
column. It means that you are creating theblog_id
column with a foreign key constraints so the columns in your table areid
,content
,status
,blogun_id
,profilin_id
,blog_id
, andprofile_id
. Then when you are saving a data the only columns that are adding a data iscontent
,status
,blogun_id
,profilin_id
which makes theblog_id
andprofile_id
null which produce a error since the 2 columns are notnullable
In your migration file, you created 4 ids
However in your save code, you saved both
blogun_id
andprofilin_id
but notblog_id
andprofile_id
. Make sure that you added it in your code:Don’t forget to include those ids in the
fillable
if yourComments
model