我想为登录用户添加密码更新选项,因此我使用了以下代码
controller auth \ authController.php
Notice: Trying to get property of non-object in C:\xampp\htdocs\add.php on line 24
Notice: Trying to get property of non-object in C:\xampp\htdocs\add.php on line 25
Notice: Trying to get property of non-object in C:\xampp\htdocs\add.php on line 26
Notice: Trying to get property of non-object in C:\xampp\htdocs\add.php on line 27
路线
public function updatePassword()
{
$user = Auth::user();
$rules = array(
'old_password' => 'required',
'password' => 'required|alphaNum|between:6,16|confirmed'
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return Redirect::route('change-password', $user->id)->withErrors($validator);
} else {
if (!Hash::check(Input::get('old_password'), $user->password)) {
return Redirect::route('change-password', $user->id)->withErrors('Your old password does not match');
} else {
$user->password = Input::get('password');
$user->save();
return Redirect::route('change-password', $user->id)->with("message", "Password have been changed");
}
}
}
我得到以下错误
Route::post('change-password', 'Auth\AuthController@updatePassword');
Route::get('change-password', 'Auth\AuthController@updatePassword');
这一行" $ user = Auth :: user();"
答案 0 :(得分:1)
你的问题有隐藏的答案..我有类似的问题,比如@ faz ..我已经用他的问题代码完成了伎俩
实现这一目标的正确方法 -
protected function postChangePassword(ChangePasswordFormRequest $request){
$user = Auth::user();
$current_password = Input::get('current_password');
$password = bcrypt(Input::get('password'));
$user_count = DB::table('users')->where('id','=',$this->user_id)->count();
if (Hash::check($current_password, $user->password) && $user_count == 1) {
$user->password = $password;
try {
$user->save();
$flag = TRUE;
}
catch(\Exception $e){
$flag = FALSE;
}
if($flag){
return redirect('/u/0/change/password')->with('success',"Password changed successfully.");
}
else{
return redirect('/u/0/change/password')->with("danger","Unable to process request this time. Try again later");
}
}
else{
return redirect('/u/0/change/password')->with("warning","Your current password do not match our record");
}
}
请注意Hash和Auth,我们需要在顶部包含类,而user_id需要通过构造函数$this->user_id = Auth::user()->id;
。我想我帮助了人们。
答案 1 :(得分:0)
其名称空间问题,请尝试:
//if this method is not protected by a middleware for only authenticated users
//verify that user is currently logged in:
if(!$user = \Auth::user()) abort(503); //or return redirect()->route('login')
$rules = array(
'old_password' => 'required',
'password' => 'required|alphaNum|between:6,16|confirmed'
);
或者在AuthController
use Auth;
class AuthController{
....
}
答案 2 :(得分:0)
您没有导入Auth类。
在文件顶部添加此项。在命名空间之后。
使用Illuminate \ Support \ Facades \ Auth;
答案 3 :(得分:0)
据我所知,您只使用laravel的auth命名空间,只需在控制器文件顶部写入此行
use Auth;
将解决您的问题。