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

simangae's avatar

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!!');

}
0 likes
11 replies
zoltiecodes's avatar

Hi. Try to remove the array from your update method call. Like this: $user->student()->update($profile);

simangae's avatar

Hi, Thanks But I tried that before and it produced this error

Type error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::update() must be of the type array, object given, called in..

Hence I tried to wrap it around array.

zoltiecodes's avatar

Then try this :) $user->student()->update($profile->toArray()); or create the array yourself like this:

$user->student()->update([
  'date_of_birth' => $request->date_of_birth,
  // And so on....
]);
2 likes
simangae's avatar

Now No errors but it's not doing anything, It doesn't update

simangae's avatar

Hi, It's Filled in..

class Student extends Model { protected $fillable = [ 'date_of_birth', 'id_number', 'gender', 'phone','address', ];

}

and the user model

protected $fillable = [ 'name', 'email', 'password','pro_pic', 'role','status', ];

and the Form

                      @csrf
                      @method('PUT')
                      
                    <fieldset>
                    <legend>Account Information</legend>
                    <div class="form-group">
                    <div class="picture-container">
                            <div class="picture">
                                <img src="/uploads/avatars/{{ $user->pro_pic }}" class="picture-src" id="wizardPicturePreview" title="">
                                <input name="pro_pic" id="wizard-picture" class="" type="file">
                            </div>
                             <h6 class="">Choose Picture</h6>
                    
                    </div>
                </div>
                <div class="container">
                    <div class="form-group col-md-5 ml-5">
                            
                        <div class="input-group">                         
                            <span class="input-group-text pt-4 "><i class="fa fa-user text-right"></i></span>
                            <input type="text" name="name" id="" class="form-control" value={{ $user->name }}>                           
                        </div>
                    </div>

                    <div class="form-group col-md-5 ml-5">
                        <div class="input-group">                         
                            <span class="input-group-text pt-4 "><i class="fa fa-envelope text-right"></i></span>
                            <input type="email" name="email" id="" class="form-control"  value={{ $user->email }}>                           
                        </div>
                    </div>

                    <div class="form-group col-md-5 ml-5">
                        <div class="input-group">                         
                            <span class="input-group-text pt-4 "><i class="fa fa-lock text-right"></i></span>
                            <input type="password" name="password" id="" class="form-control" placeholder="Password">                           
                        </div>
                    </div>

                    <div class="form-group col-md-5 ml-5">
                        <div class="input-group">                         
                            <span class="input-group-text pt-4 "><i class="fa fa-check text-right"></i></span>
                            <input type="password" name="confirm_password" id="" class="form-control" placeholder="Confirm Password">                           
                        </div>
                    </div>

                    <div class="form-group col-md-5 ml-5">
                        {{--  <label for="role">Select User Role</label>  --}}
                        
                            <select name="role"  class="form-control " data-style="btn-primary btn-outline">
                                
                                <option data-tokens="User" value="1">User</option>
                                <option  data-tokens="Admin" value="2">Admin</option>
                                <option data-tokens="user" value="3">Student</option>
                            </select>                           
                    </div>

                    <div class="form-group col-md-5 ml-5">
                            
                        <select name="status"  class="form-control " data-style="btn-primary btn-outline">
                                
                            <option data-tokens="User" value="1">Active</option>
                            <option  data-tokens="Admin" value="0">Disabled</option>
                        
                        </select> 
                                                          
                    </div>


                    </fieldset>
                    
                    <fieldset>
                        <legend>user Information</legend>
                        <div class="form-group col-md-5 ml-5">
                        <div class="input-group">
                            <span class="input-group-text pt-4 "><i class="fa fa-calendar text-right"></i></span>
                            <input type="text" name="date_of_birth" class="form-control mydatepicker"  value="{{ $user->student['date_of_birth'] }}">
                            
                        </div>
                        </div>

                        <div class="form-group col-md-5 ml-5">
                            <div class="input-group">                         
                                <span class="input-group-text pt-4 "><i class="fa fa-hashtag text-right"></i></span>
                                <input type="text" name="id_number" id="" class="form-control"  value="{{ $user->student['id_number'] }}">                           
                            </div>
                        </div>

                        <div class="form-group col-md-11 ml-5">
                                <div class="input-group">                         
                                    <span class="input-group-text pt-4 "><i class="fa fa-home text-right"></i></span>
                                    <textarea name="address" id="" class="form-control" placeholder="Address">{{ $user->student['address'] }}</textarea>                           
                                </div>
                            </div>

                        <div class="form-group col-md-5 ml-5">
                            <div class="input-group">                         
                                <span class="input-group-text pt-4 "><i class="fa fa-phone text-right"></i></span>
                                <input type="text" name="phone" id="" class="form-control"  value="{{ $user->student['phone'] }}">                           
                            </div>
                        </div>

                        <div class="form-group col-md-11 ml-5">
                                <div class="input-group">                         
                                    <span class="input-group-text pt-4 "><i class="fa fa-history text-right"></i></span>
                                    <textarea name="bio" id="" class="form-control" placeholder="Address">Bio</textarea>                           
                                </div>
                            </div>

                        
                        <div class="form-group col-md-11 ml-5">
                            <button class="btn btn-block btn-rounded" type="submit"><i class="fa fa-paper-plane"></i><span>Submit</span> </button>  
                        </div>

                        
                    </fieldset>
                </div>
                 
                   
                    
                </form>    

I also changed @method('PUT') to @method('PATCH').. Still didn't work..

the update method

public function edit($id) { $user = User::findOrFail($id); return view('students.edit', ['user' => $user]); }

public function update(Request $request, $id)
{
    $user = User::find($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($profile->toArray());
    return redirect()->route('students.index')->with('update', 'Student Has been updated!!');

}

Not really sure what I'm doing wrong

simangae's avatar

method="POST" Action="{{ route('students.update', $user->id)}}"

zoltiecodes's avatar
Level 14

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.