Laravel empty password hashed on user update
You can use in this case:
Input::except('password')
so in your controller you can do it like this:
if (trim(Input::get('password')) == '') {
$data = Input::except('password');
}
else {
$data = Input::all();
}
$user->update($data);
However, you should consider other possible problems for this. In this case, if a user submits input with a name id
(and anyone can do it, even if you don't have such a field in your form), he can easily change other user / account passwords and destroy all your data.
You should use at User
least in your model:
protected $guarded = array('id');
to protect the user ID from being changed during bulk assignment , but there may be other fields that you would like to protect (you must list them in an array $guarded
.
For me, a much better option in this case is to use a standard custom update:
$user = User::find($id);
if (trim(Input::get('password')) != '') {
$user->password = Hash::make(trim(Input::get('password')));
}
$user->name = Input::get('name');
// and so on - this way you know what you are changing and you won't change something you don't want to change
$user->save();
source to share
Just as Tom Bird commented, here is some sample code for example.
If you are using a type mutator in your model setPasswordAttribute()
, then you can do this:
public function setPasswordAttribute($password)
{
if (!empty($password))
{
$this->attributes['password'] = bcrypt($password);
}
}
This will prevent the new password from being hashed. This setPasswordAttribute () method is called a "mutator" and became available in Laravel 4.2 from what I see. http://laravel.com/docs/4.2/eloquent
source to share