0

I'm trying to do simple function edit student table field, But its updating only first field: first_name

My controller.

    public function editStudent(Request $request)
{
    $studId = $request->get('studId');
    $studFirstName = $request->get('firstName');
    $studLastName = $request->get('lastName');
    $studGender = $request->get('gender');
    $studBirthday = $request->get('birthday'); 
    $studSchoolId = $request->get('schoolId'); 
    $update = Student::where('id', $studId)->update(['first_name' => $studFirstName],
    ['last_name' => $studLastName], ['gender' => $studGender], ['birthday', $studBirthday], ['school_id' => $studSchoolId]); 
    return response()->json([
        "update" => $update,
    ]);
}
0

2 Answers 2

3

Update takes a single array, you're passing in multiple arrays

$update = Student::where('id', $studId)
    ->update([
        'first_name' => $studFirstName,
        'last_name' => $studLastName, 
        'gender' => $studGender, 
        'birthday'=> $studBirthday, 
        'school_id' => $studSchoolId
    ]);
Sign up to request clarification or add additional context in comments.

2 Comments

I tried that but it's errors SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list'
Ah, there's an extra comma instead of an arrow between birthday and studBirthday. I've edited my answer to fix hat
0

Its better to get the item from DB then update that on a second query:

$student = Student::find($studId);

$student->update([
    'first_name' => $request->firstName,
    'last_name' => $request->lastName, 
    'gender' => $request->gender, 
    'birthday'=> $request->birthday, 
    'school_id' => $request->schoolId
]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.