Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to solve this General error: 1364 Field doesn't have a default value

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading