I was trying to change the user's password, but it did not work, someone can help me I'm new to Laravel.
public function updatePassword()
{
$user = Auth::user();
$user->password = bcrypt($request['newpassword']);
$user->save();
}
The solution I found to the problem was this
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Validator;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
use Image;
use Hash;
use App\User;
public function password(){
return View('user.password');
}
public function updatePassword(Request $request){
$rules = [
'mypassword' => 'required',
'password' => 'required|confirmed|min:6|max:32',
];
$messages = [
'mypassword.required' => 'Campo obrigatorio',
'password.required' => 'Campo obrigatorio',
'password.confirmed' => 'As senhas de redefinição não são iguais',
'password.min' => 'A senha deve possuir no mínimo 6 caracteres.',
'password.max' => 'A senha deve possuir no maxímo 32 caracteres.',
];
$validator = Validator::make($request->all(), $rules, $messages);
if ($validator->fails()){
return redirect('user/password')->withErrors($validator);
}
else{
if (Hash::check($request->mypassword, Auth::user()->password)){
$user = new User;
$user->where('email', '=', Auth::user()->email)
->update(['password' => bcrypt($request->password)]);
return redirect('user/password')->with('status', 'Senha alterada com sucesso');
}
else
{
return redirect('user/password')->with('message', 'O token para troca de senha é inválido.');
}
}
}