#SQLSTATE[HY000]: General error: 1364 Field 'attended_nurse' doesn't have a default value

21 messages · Page 1 of 1 (latest)

inland ember
#

public function store(NursingProcedureRequest $request)
{
$validated_data = collect($request->validated())->except('procedure','attended_nurse','detail')->toArray();
$nursing_procedures = NursingProcedure::create($validated_data);

    $nursing_procedure_id = $nursing_procedures->id;

    $procedure = $request->validated('procedure');
    $details = $request->validated('details');
    $attended_nurse = $request->validated('attended_nurse');


    foreach ($procedure as $index => $procedure_id) {
        $nursingProcedureMedicine = new MedicineNursingProcedure();
        $nursingProcedureMedicine->nursing_procedure_id = $nursing_procedure_id;
        $nursingProcedureMedicine->medicine_id = $procedure_id;
        $nursingProcedureMedicine->detail = $details[$index];
        $nursingProcedureMedicine->attended_nurse = $attended_nurse;
        $nursingProcedureMedicine->save();
    }


    return redirect()->route('nursing-procedures.index');
}

The error above keeps happening

#

and how did you knew it was before the forloop?

#

i mean how did u find out from the that error?

brittle frost
#

those three fields are from dynamic inputs
you mentioned those fields are dynamic, but you need the data to create the NursingProcedure

#

I have no idea what's your business logic, but yeah, your have removed the data needed at this line, which is the attented_nurse

$validated_data = collect($request->validated())->except('procedure','attended_nurse','detail')->toArray();
inland ember
#

MedicineNursingProcedure with this model

brittle frost
inland ember
#

oh no

#

i think i knw what is wrong

brittle frost
inland ember
#

public function up(): void
    {
        Schema::create('nursing_procedure', function (Blueprint $table) {
            $table->id();
            $table->foreignId('patient_id')->constrained()->cascadeOnDelete();
            $table->foreignId('attended_nurse')->constrained('users')->cascadeOnDelete();
            $table->text('diagnosis');
            $table->dateTime('nursing_procedure_done_date');
            $table->timestamps();
        });
    }```
#

i didnt remove it from the migration

brittle frost
#

if it is not needed, then just create a new migration file to remove the foreign key

inland ember
#

I will try it. THank you so much

#

and

#

one more thing.

#

foreach ($procedure as $index => $procedure_id) {
$nursingProcedureMedicine = new MedicineNursingProcedure();
$nursingProcedureMedicine->nursing_procedure_id = $nursing_procedure_id;
$nursingProcedureMedicine->medicine_id = $procedure_id;
$nursingProcedureMedicine->detail = $details[$index];
$nursingProcedureMedicine->attended_nurse = $attended_nurse;
$nursingProcedureMedicine->save();
}

#

is there any way to make this foreach more clean?

brittle frost
#

no idea, but thinks you can implement bulk insert on that

#

btw, I don't think the codes will work as $attended_nurse is an array. so you might forgotten to access it via $index