Hello everyone, I can't change my user password, but other field. i dont know why, plz help me . what i miss?
My code: User Model: public function setPasswordAttribute() { $this->password = Hash::make($this->password); }
Route::get('/', function() { $user = User::find(39); $user->password = 'aaaaaaaa'; $user->email = 'john@foo.com'; $user->save(); echo $user->getAuthPassword(); dd($user); });
return:
$2y$10$/dykL277lqSyXEXxA8jZf.dIgAmgdORnTZTCJHNEoNs60VAVVuDB2object(User)#201 (21) { ["table":protected]=> string(4) "user" ["timestamps"]=> bool(true) ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(39) ["group_id"]=> int(1) ["username"]=> string(5) "marka" ["email"]=> string(12) "john@foo.com" ["password"]=> string(0) "" ["ip"]=> string(9) "127.0.0.1" ["active"]=> int(0) ["activation_code"]=> string(0) "" ["remember_token"]=> NULL ["last_login_date"]=> string(19) "2014-05-17 17:45:55" ["created_at"]=> string(19) "2014-05-17 17:45:55" ["updated_at"]=> string(19) "2014-05-17 18:02:56" } ["original":protected]=> array(12) { ["id"]=> int(39) ["group_id"]=> int(1) ["username"]=> string(5) "marka" ["email"]=> string(12) "john@foo.com" ["password"]=> string(0) "" ["ip"]=> string(9) "127.0.0.1" ["active"]=> int(0) ["activation_code"]=> string(0) "" ["remember_token"]=> NULL ["last_login_date"]=> string(19) "2014-05-17 17:45:55" ["created_at"]=> string(19) "2014-05-17 17:45:55" ["updated_at"]=> string(19) "2014-05-17 18:02:56" } ["relations":protected]=> array(0) { } ["hidden":protected]=> array(0) { } ["visible":protected]=> array(0) { } ["appends":protected]=> array(0) { } ["fillable":protected]=> array(0) { } ["guarded":protected]=> array(1) { [0]=> string(1) "*" } ["dates":protected]=> array(0) { } ["touches":protected]=> array(0) { } ["observables":protected]=> array(0) { } ["with":protected]=> array(0) { } ["exists"]=> bool(true) ["softDelete":protected]=> bool(false) ["password"]=> string(60) "$2y$10$/dykL277lqSyXEXxA8jZf.dIgAmgdORnTZTCJHNEoNs60VAVVuDB2" }
What happens if you try:
Route::get('/', function() {
$user = User::find(39);
echo ' Before Save: ' . $user->getAuthPassword();
$user->email = 'john@foo.com';
$user->password = Hash::make('aaaaaaaa');
$user->save();
echo ' After Save: ' . $user->getAuthPassword();
dd($user);
});
The User model encrypts your password when saving it to the database, your password will not be in plain text.
thanks for help, i know we have to encrypts passwd before save so i have put that code public function setPasswordAttribute() { $this->password = Hash::make($this->password); }
still can't change the password, but the other field
result: Before Save: $2y$10$47fSa//7DFWx0cv61PypZeCaACf4r.OxdrtThG.TD1kkS7HJIFB1m After Save: $2y$10$3qCjAitWwGqqQSaKgTRK6O9/RolQ7Ep/uEVmQHplNTJKF6dXITb4Gobject(User)#201 (21) { ["table":protected]=> string(4) "user" ["timestamps"]=> bool(true) ["connection":protected]=> NULL ["primaryKey":protected]=> string(2) "id" ["perPage":protected]=> int(15) ["incrementing"]=> bool(true) ["attributes":protected]=> array(12) { ["id"]=> int(1) ["group_id"]=> int(1) ["username"]=> string(5) "admin" ["email"]=> string(20) "john@foo.com" ["password"]=> string(60) "$2y$10$47fSa//7DFWx0cv61PypZeCaACf4r.OxdrtThG.TD1kkS7HJIFB1m"
Try it without setPasswordAttribute. Try the example exactly as I have, just to be sure it is not your setPasswordAttribute method.
Thanks pickupman, it's ok now. i dont know why seypasswordattribute dont work when i updated password
so if i need login,register, update which about password field, i just use Hash::make to do it? DONT USE setPasswordAttribute() ?
have other suggests ?
It's because your mutator is wrong and should look like this to work:
public function setPasswordAttribute($plain)
{
$this->attributes['password'] = Hash::make($plain);
}
pickupman said:
What happens if you try:
Route::get('/', function() { $user = User::find(39); echo ' Before Save: ' . $user->getAuthPassword(); $user->email = 'john@foo.com'; $user->password = Hash::make('aaaaaaaa'); $user->save(); echo ' After Save: ' . $user->getAuthPassword(); dd($user); });
The User model encrypts your password when saving it to the database, your password will not be in plain text.
Is this an appropriate way ?
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community