Hi. Try to remove the array from your update method call. Like this: $user->student()->update($profile);
Update hasOne relationship
Need help, trying to update two tables that has one to one relationship, the store method is working fine but when i try to do update I'm getting the following error..
SQLSTATE[42S22]: Column not found: 1054 Unknown column '0' in 'field list' (SQL: update students set 0 =
My Store Method which is working fine
public function store(Request $request) { $user = new User; $user->name = $request->name; $user->email = $request->email; $user->password = Hash::make($request->password); $user->role = $request->role; $user->status = $request->status;
if($request->hasFile('pro_pic')) {
$image = $request->file('pro_pic');
$filename = time() . '.' .$image->getClientOriginalExtension();
$location = public_path('uploads/avatars/' .$filename);
Image::make($image)->resize(300, 300)->save($location);
$user->pro_pic = $filename;
}
$user->save();
$profile = new Student;
$profile->date_of_birth = $request->date_of_birth;
$profile->id_number= $request->id_number;
// $profile->gender = $request->gender;
$profile->phone= $request->phone;
$profile->address = $request->address;
$user->student()->save($profile);
return redirect()->back()->with('success', 'New Student was Added!!');
}
Then The update method which is producing the error.
public function edit($id) { $user = User::findOrFail($id); return view('students.edit', ['user' => $user]); }
public function update(Request $request, $id)
{
$user = User::findOrFail($id);
$user = new User;
$user->name = $request->name;
$user->email = $request->email;
$user->password = Hash::make($request->password);
$user->role = $request->role;
$user->status = $request->status;
if($request->hasFile('pro_pic')) {
$image = $request->file('pro_pic');
$filename = time() . '.' .$image->getClientOriginalExtension();
$location = public_path('uploads/avatars/' .$filename);
Image::make($image)->resize(300, 300)->save($location);
$user->pro_pic = $filename;
}
$user->update();
$profile = new Student;
$profile->date_of_birth = $request->date_of_birth;
$profile->id_number= $request->id_number;
// $profile->gender = $request->gender;
$profile->phone= $request->phone;
$profile->address = $request->address;
$user->student()->update(array($profile));
return redirect()->route('students.index')->with('update', 'Student Has been updated!!');
}
Hi. The problem is that you find your User, and just after that you create a new User. Remove the $user = new User; line. @simangae
$user = User::find($id);
$user = new User; // <-- Remove this line.
Please or to participate in this conversation.