1

I'm trying to update the password of a user I've pulled from the database in Laravel 4. Here is what I have:

$user = User::where('username', $username)->get();
if (!empty($user)) {
    $user->password = Hash::make($password);
    $user->save();
}

But Laravel is telling me save is an unknown method. What am I doing wrong here?

2 Answers 2

8

get returns an array of users. You need to use first to get a single user object that will allow you to adjust the password and save the record.

if ($user = User::where('username', $username)->first())
{
    $user->password = Hash::make($password);

    $user->save();
}
Sign up to request clarification or add additional context in comments.

Comments

0

Are you sure $user actually has a 'user' in it?

It wont be 'empty' - it will be BOOL false. Try var_dump($user) and confirm something is returned?

Try this:

$user = User::where('username', $username)->get();
var_dump($user);
if ($user) {
    $user->password = Hash::make($password);
    $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.