0

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();

2 Answers 2

1

Since you are using user_id as a foreign key in the profiles table, you would expect this as your statement:

Profile::where('user_id', $user->id)->first();

Or, by using the relation:

$user->profile;
Sign up to request clarification or add additional context in comments.

Comments

0

You can just use your relation and also compress the logic as :

$user->name             = $validated['name'];
$user->email            = $validated['email'];

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

    $user->password   = bcrypt($validated['password']);

    $profile  = $user->profile;
    $profile->username      = $validated['name'];
    $profile->email         = $validated['email'];
    $profile->save();
}
$user->save();

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.