Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TikTina's avatar

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

What could possibly be the problem? SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into medical_records (level_of_physical_activity, blood_pressure, body_temperature, heart_rate, respiration_rate, height, weight, additional_notes, updated_at, created_at) values (medium, 43322, 432, 43, 32, 32, 34, 342, 2021-12-12 13:10:56, 2021-12-12 13:10:56))

0 likes
21 replies
Sinnbeck's avatar

Can you show your php code. Sounds like you aren't setting the user_id

TikTina's avatar

@Sinnbeck

​```

class HealthPredispositionQuestionnaireController extends Controller { public function index() { return Inertia::render('HealthPredispositionQuestionnaire/Index', [ 'questionnaire' => HealthPredispositionQuestionnaire::all(), ]); }

public function questionnaireForm(Request $request)
{

    $this->validate($request, [
        'past_medical_problems' => 'required',
        'current_medications' => 'required',
        'level_of_physical_activity' => 'required',
        'blood_pressure' => 'required',
        'body_temperature' => 'required',
        'heart_rate' => 'required',
        'respiration_rate' => 'required',
        'height' => 'required',
        'weight' => 'required',
        'additional_notes' => 'required',

    ]);
    $request->merge([
       'user_id'=>\Auth::user()->id
    ]);

    $record = MedicalRecord::create($request->all());

    return Inertia::render('SymptomsQuestionnaireQuestionnaire/Index', [
        'medicalRecordId' => $record->id
    ]);

}

}

​```

Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@TikTina ok. Do this

$validated = $this->validate($request
//rest of your validation
$validated['user_id'] =\Auth::user()->id;
$record = MedicalRecord::create($validated);
Sinnbeck's avatar

And never return a view after creating. Always redirect

return redirect()->back();//or similar 
1 like
TikTina's avatar

@Sinnbeck it is

    use HasFactory;
    public $fillable = [
        'past_problems_and_surgeries',
        'current_medications_usage',
        'level_of_physical_activity',
        'blood_pressure',
        'body_temperature',
        'heart_rate',
        'respiration_rate',
        'height',
        'weight',
        'additional_notes',
        'symptoms',
        'when_problem_started',
        'pain_area',
        'pain_intensity',
        'additional_symptoms',
    ];
}

Sinnbeck's avatar

@TikTina no?

public $fillable = [
        'past_problems_and_surgeries',
        'current_medications_usage',
        'level_of_physical_activity',
        'blood_pressure',
        'body_temperature',
        'heart_rate',
        'respiration_rate',
        'height',
        'weight',
        'additional_notes',
        'symptoms',
        'when_problem_started',
        'pain_area',
        'pain_intensity',
        'additional_symptoms',
'user_id' //added
    ]; 
TikTina's avatar

@Sinnbeck Now the error is not displayed, but the form is still not being sent to the database? I am sorry for taking your time...

Sinnbeck's avatar

@TikTina no worries :) let's do some debugging

What is the result of this?

$record = MedicalRecord::create($validated);
dd($record);
TikTina's avatar

@Sinnbeck


App\Models\MedicalRecord {#1411 ▼
  +fillable: array:16 [▶]
  #connection: "mysql"
  #table: "medical_records"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: true
  #attributes: array:12 [▶]
  #original: array:12 [▶]
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #guarded: array:1 [▶]
}

TikTina's avatar

@Sinnbeck Yes now it is saved in the database... But just one more thing: Now I put return redirect()->back(); over

return Inertia::render('SymptomsQuestionnaireQuestionnaire/Index', [
  'medicalRecordId' => $record->id

it is not directing me to the other form, but it stays on the same page. Because the main idea is as this form is sent it should automatically direct the user to the next one... like a wizard form/

Sinnbeck's avatar

@TikTina the you redirect to that page. It was just an example

This will redirect to a page named page2 with the $record in the url

return redirect()->route('page2', $record);
TikTina's avatar

@Sinnbeck So just to be sure, now my query should look like this:

    public function questionnaireForm(Request $request)
    {

       $validated =  $this->validate($request, [
            'past_medical_problems' => 'required',
            'current_medications' => 'required',
            'level_of_physical_activity' => 'required',
            'blood_pressure' => 'required',
            'body_temperature' => 'required',
            'heart_rate' => 'required',
            'respiration_rate' => 'required',
            'height' => 'required',
            'weight' => 'required',
            'additional_notes' => 'required',

        ]);

        $validated['user_id'] =\Auth::user()->id;
        $record = MedicalRecord::create($validated);
        

        return redirect()->route('symptoms', $record);



    }

}

Because I changed to symptoms which is the route of my next page, and it still stays at this one...

Sinnbeck's avatar

@TikTina but this route is probably a post route and it should redirect to a get route. Consider a fresh thread on how to redirect from one page to another

Snapey's avatar

You could also consider this strategy to set the user_id

        //$validated['user_id'] =\Auth::user()->id;
        $record = Auth::user()->medicalRecord()->create($validated);

assuming the User model has a medicalRecord relationship

Please or to participate in this conversation.