Can you show your php code. Sounds like you aren't setting the user_id
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))
Here is my migration: https://imgur.com/a/ANNpBKk
@TikTina I mean the code that does this query. Please copy paste
```
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
]);
}
}
```
@TikTina ok. Do this
$validated = $this->validate($request
//rest of your validation
$validated['user_id'] =\Auth::user()->id;
$record = MedicalRecord::create($validated);
And never return a view after creating. Always redirect
return redirect()->back();//or similar
@Sinnbeck The error is still present :/
@TikTina check the model to ensure it is in the $fillable array
@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',
];
}
@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
];
@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...
@TikTina no worries :) let's do some debugging
What is the result of this?
$record = MedicalRecord::create($validated);
dd($record);
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 looks like it was saved. Try
dd($record->id);
@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/
@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);
@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...
@TikTina I dont know your routes, so it's hard to know :)
@Sinnbeck Ok, thank you very much for all your help, I really appreciate it!
@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
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.