I have an error in my update user admin form where I want to update the profile that's corresponding to the user if he has a profile. But it throws this error:

> 
> Creating default object from empty value
> 
> 
> 

I have set relationships in my model between the User and profile `Profile::where('id', $user->profile_id)->first();` but when I `dd` it's always a null, even if there is a corresponding profile.

User.php

```
public function profile()

{

    return $this->hasOne('App\Profile', 'user_id', 'id');

}

```

Profile.php

```
public function user()

{
    return $this->belongsTo('App\User');

}

```

function error is in

```
public function update(Request $request, User $user)
{
    if(\Auth::check()) {

        if(\Auth::user()->type == 'admin') {

            $validated = $request->validate([

                'name' => 'required',

                'email' => 'required|email',

                'password' => 'confirmed'
            ]);

            if(!empty($validated['password'])){

                if(!$user->profile){
                    //Has no profile
                    $user->name             = $validated['name'];
                    $user->email            = $validated['email'];
                    $user->password         = bcrypt($validated['password']);
                    $user->update();
                } else {
                    //Has profile
                    $profile                = Profile::where('id', $user->profile_id)->first();
                    $profile->username      = $validated['name'];
                    $profile->email         = $validated['email'];
                    $profile->update();

                    $user->name             = $validated['name'];
                    $user->email            = $validated['email'];
                    $user->password         = bcrypt($validated['password']);
                    $user->update();
                }

            } else {

                if(!$user->profile){
                    //Has no profile
                    $user->name             = $validated['name'];
                    $user->email            = $validated['email'];
                    $user->update();
                } else {
                    //Has profile
                    $profile                = Profile::where('id', $user->profile_id)->first();
                    $profile->username      = $validated['name'];
                    $profile->email         = $validated['email'];
                    $profile->update();

                    $user->name             = $validated['name'];
                    $user->email            = $validated['email'];
                    $user->update();
                }
            }
        }
    }
}

```

on these lines `$profile = Profile::where('id', $user->profile_id)->first();`