SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'job_description' cannot be null (SQL: insert into jobs
(job_title
, job_description
, file
, note
) values (EEE, ?, user.JPG, KJL))
migration
{
Schema::create('jobs', function (Blueprint $table) {
$table->id();
$table->String('job_title');
$table->String('job_description');
$table->String('file');
$table->String('note');
$table->timestamps();
});
}
model
{
use HasFactory;
protected $fillable = [
'job_title',
'job_description',
'file',
'note',
];
controller
Job::insert([
'job_title'=>$request->job_title,
'job_description'=>$request->job_description,
'file'=>$request->file,
'note'=>$request->note
]);
return Redirect()->back()->with('success','Job added successfully');
}
CodePudding user response:
The column job_description in the database is set up as NOT NULL constraints. And the value trying to set using $request->job_description with NULL. That is the root cause of this issue.
To rectify you can modify your controller like:
Job::insert([
'job_title'=>$request->job_title,
'job_description'=>(!is_null($request->job_description) ? $request->job_description : ""),
'file'=>$request->file,
'note'=>$request->note
]);
Or provide a default value for fields using input()
Job::insert([
'job_title' => $request->job_title,
'job_description' => $request->input('job_description', ''),
'file' => $request->file,
'note' => $request->note
]);
CodePudding user response:
Use nullable
for column declaration $table->string('job_description')->nullable()
, if you want use it with null value.