I have a form to register tasks, the migrate file is as follows:
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('Caption',300)->nullable();
$table->string('TaskDsc',300)->nullable();
$table->unsignedBigInteger('office_id');
$table->foreign('office_id')->references('id')->on('offices');
$table->date('TaskDate');
$table->boolean('Type')->comment('0 for command and 1 for session') ;
$table->unsignedBigInteger('Priority_id');
$table->foreign('Priority_id')->references('id')->on('priorities');
$table->string('TaskFile',100)->nullable();
$table->date('StartDate');
$table->date('EndDate');
$table->smallInteger('Deadline')->comment('to day');
$table->unsignedBigInteger('Commander_id');
$table->foreign('Commander_id')->references('id')->on('commanders');
$table->json('OtherOffices')->nullable();
$table->unsignedBigInteger('Secretary_id')->nullable();
$table->foreign('Secretary_id')->references('id')->on('offices');
$table->smallInteger('SessionRowNo')->nullable();
$table->unsignedBigInteger('Status_id');
$table->foreign('Status_id')->references('id')->on('statuses');
$table->timestamps();
});
}
and model file this code:
protected $fillable = [
'id',
'caption',
'taskDsc',
'TaskDate',
'type',
'priority_id',
'taskfile',
'startDate',
'endDate',
'deadline',
'office_id',
'commander_id',
'otheroffices',
'Status_id'
];
and service file:
class TaskService
{
public function create($data)
{
return task::create($data);
}
public function update($task, $data)
{
return $task->update($data);
}
}
and controller:
public function store(taskRequestStore $request,TaskService $service)
{
// dd($request);
$attributes = $request->all(['Caption','TaskDsc',
'office_id','TaskDate','Priority_id',
'Type','Taskfile','StartDate','EndDate','Deadline',
'Commander_id','OtherOffices','Status_id'
]);
$office = $service->create($attributes)->tosql();
}
when fill blade form and send form to store ,i recive this error:
SQLSTATE[HY000]: General error: 1364 Field 'Type' doesn't have a default value (SQL: insert into `tasks` (`office_id`, `TaskDate`, `Status_id`, `updated_at`, `created_at`) values (1, 2024-02-27, 1, 2024-02-27 12:31:22, 2024-02-27 12:31:22))
By activating the dd command in controller, the value of the request is as follows:
+request: Symfony\Component\HttpFoundation\InputBag {#1317 ▼
#parameters: array:13 [▼
"_token" => "JhLFgL6yLxNbH0ye6A9QQoSf3xIN9BpCBEsCSl5J"
"Caption" => "this is caption"
"TaskDate" => "۱۴۰۲-۱۲-۰۸"
"type" => "1"
"Priority_id" => "1"
"taskDsc" => "<p><span style="background-color:rgb(255,255,255);color:rgb(33,37,41);">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididun ▶"
"formFile" => null
"startDate" => null
"deadline" => null
"office_id" => "1"
"otherOffices" => array:1 [▶]
"commander_id" => "1"
"Status_id" => "1"
]
}
It seems that type has a value and there is no problem, but this error is issued. Of course, by giving the initial value to type, the error will be displayed for the priority_id field this time
>Solution :
Seems like you are using a mix of snake case, camel case and anything in between.
Your $fillable does not match your database or what you are trying to retrieve and insert.
So you are trying to insert it into "Type", however, your $fillable is "type" (Notice the capital letter difference).
My recommendation is to keep every data variable as snake case "type", "start_date", "commander_id", etc.
Hope this was helpful.