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?
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();
}
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();
}
Related
I have this mechanism of checking the logged-in user role in a blade page so that I can give him/ her the selected pages but I find after a user has logged in and stayed idle for a long time and when he is back and wants to continue in the page he is getting an error
ErrorException
Trying to get property 'id' of non-object (View: /var/www/html/poss_v1/resources/views/dashboard/student/index.blade.php)
and my code is here below
<?PHP
$user = Auth::user();
$user_id = $user->id; //----here is the problem it is missing the id when the session has ended--//
$role_user = App\Models\RoleUser::where('user_id', $user_id)->first();
$role_name = App\Models\Role::where('id', $role_user->role_id)->first();
$role = $role_name->name;
?>
#php
if(Auth::check()){
$role_user_id = App\Models\RoleUser::where('user_id', auth()->id())->first()->role_id;
$role = App\Models\Role::where('id', $role_user_id)->first()->name;
}
#endphp
In Laravel blade file you should check logged in user in this way.
#php
if(Auth::check()){
$user = Auth::user();
$user_id = $user->id;
$role_user = App\Models\RoleUser::where('user_id', $user_id)->first();
$role_name = App\Models\Role::where('id', $role_user->role_id)->first();
$role = $role_name->name;
}
#endphp
To increase the lifetime of your session, you may modify the env variable SESSION_LIFETIME to whatever minutes you wish.
And to make sure your user doesn't get this error, you may check if the session is still alive with Auth::check()
I have small problem in app. Backend is Laravel and Front end is Nuxtjs.
When User registered on app,then users must be wait antill Administartor approved.
When Admin approved this user we give them 3 months subscription.
In my code this part is not working.
$user->activated_at = now();
$user->activated_at = date('Y-m-d H:i');
{
$user = User::find($id);
if (is_null($user)) {
return $this->sendError('admin_messages.user_not_found');
}
$user->status = User::ACTIVE;
$user->activated_at = now();
$user->activated_at = date('Y-m-d H:i');
dd($user->activated_at);
$user->save();
Mail::to($user->email)->queue(new ApproveNotificationMail($user));
return $this->sendResponse('admin_messages.user_activated');
}
$user->activated_at = \Carbon\Carbon::now()->format('Y-m-d H:i');
You can use mutator in your User model, to force activated_at format:
public function setActivatedAtAttribute( $value ) {
$this->attributes['activated_at'] = (new Carbon($value))->format('Y-m-d H:i');
}
You can define a format for date columns using:
protected $dateFormat = 'Y-m-d H:i';
Note that you can directly use:
$user->activated_at = \Carbon\Carbon::now()->format('Y-m-d H:i');
For Laravel change password I did like this but why it is not working.. Its not updating password. I have done login page, registration everything working. But this giving me lot of trouble. Below is my code.
$returnValue = DB::table('users')->where('users_id', $users_id)->where('password', bcrypt($request->opassword))->update(['password'=>bcrypt($request->npassword)]);
if($returnValue >= 1)
{
$success['message'] = "Password updated successfully..";
return $this->sendResponse($success);
}
else
{
$error = "Entered Old password is not valid..";
return $this->sendResponse($error);
}
At first import Hash at the top of the controller. like this below
use Illuminate\Support\Facades\Hash;
After this , validate old password match with your database like this below
$user = User::findOrFail($users_id);
if (Hash::check($request->password, $user->password)) {
$user->password = Hash::make($request->opassword);
$user->update(); // $user->save();
$success['message'] = "Password updated successfully..";
return $this->sendResponse($success);
} else {
$error = "Entered Old password is not valid..";
return $this->sendResponse($error);
}
Try this.
You have issues with your keys:
$returnValue = DB::table('users')
->where('id', $users_id)
->where('password', bcrypt($request->opassword))
->update(['password'=>bcrypt($request->npassword)]);
I do not know why the auth of laravel not work to me.. I have tried all but it still does not work, I use this code to login:
$user = User::where('rut', $request->rut)
->where('password', md5($request->password))
->first();
$employee = Employee::where('id_user', $user->id_user)
->first();
$permissions = User_Type_Permission::where('id_user_type', $user->id_user_type)->pluck('id_permission')->toArray();
$request->session()->put('id_user_type', $user->id_user_type);
$request->session()->put('id_branch_office', $employee->branch_office);
$request->session()->put($permissions);
if(Auth::login($user))
{
echo 1;
die();
return redirect('/account');
}
else
{
echo 2;
die();
return redirect('/login');
}
It returns "2" every time, and the user is not empty, it comes with values from database
I have used Attempt too:
$user = User::where('rut', $request->rut)
->where('password', md5($request->password))
->first();
$employee = Employee::where('id_user', $user->id_user)
->first();
$permissions = User_Type_Permission::where('id_user_type', $user->id_user_type)->pluck('id_permission')->toArray();
$request->session()->put('id_user_type', $user->id_user_type);
$request->session()->put('id_branch_office', $employee->branch_office);
$request->session()->put($permissions);
$credentials = [
'rut' => $user->rut,
'password' => md5($request->password),
];
if(Auth::attempt($credentials))
{
echo 1;
die();
return redirect('/account');
}
else
{
echo 2;
die();
return redirect('/login');
}
and it returns false too, the login information is correct, but Auth does not work at all, what can it be?
Thanks!
You should remove md5.. if you store the hashed password by bcrypt..
Auth::attempt() automatically check the plain text password in the request with the hashed in the database.
So the code should be
$credentials = [
'rut' => $user->rut,
'password' =>$request->password,
];
if(Auth::attempt($credentials))
{
echo 1;
die();
return redirect('/account');
}else{return redirect('/login')}
And put all the code you have written in the top to the true condition..
what you write is not correct ..
if authenticated make what i want..
Not put the code then check if authenticated!
I Hope this work with you!
Good luck!
I have the following code that displays different data depending on if you are the authenticated user looking at your own data or if you are looking at data from another user. The code works but in my opinion is very nasty.
public function showUserDetailed(Request $request, $username)
{
$key = strtolower($username).'-data-detailed';
if(Auth::user()->usenrame === $username) {
$key = $key .'-own';
}
if(Cache::has($key)) {
if($request->wantsJson()) {
return request()->json(Cache::get($key));
} else {
return view('user/detailed', ['user' => Cache::get($key)]);
}
} else {
if(Auth::user()->username === $username) {
$u = User::where('username', $username)->first();
$user = new \stdClass();
$user->username = $u->username;
$user->email = $u->email;
$user->address = $u->address;
$user->city = $u->city;
$user->state = $u->state;
$user->zip = $u->zip;
$user->phone = $u->phone;
$user->follows = $u->follows;
$user->ratings = $u->ratings;
$user->location = $u->location;
} else {
$u = User::where('username', $username)->first(['username', 'city', 'state']);
$user = new \stdClass();
$user->username = $u->username;
$user->city = $u->city;
$user->state = $u->state;
$user->follows = $u->follows;
$user->ratings = $u->ratings;
}
Cache::put($key, $user);
if($request->wantsJson()) {
return response()->json($user);
} else {
return view('user/detailed', ['user' => $user]);
}
}
}
I have tried to use something similar to the following in the model calls.
User::where('username', $username)->with('follows', 'ratings', 'location')->find(['username', 'city', 'state');
However when I specify the columns to get from the user table it negates the relation data so it comes back as empty arrays. Is there a different way I can do this which would be cleaner? I despise having to create a stdClass to be a data container in this situation and feel I may be missing something.
// this works
User::where('username', $username)->with('follows', 'ratings', 'location')->first();
// this also works
User::where('username', $username)->first(['username', 'city', 'state']);
// this does not
User::where('username', $username)->with('follows', 'ratings', 'location')->first(['username', 'city', 'state']);
When you specify columns and want to get the related models as well, you need to include id (or whatever foreign key you're using) in the selected columns.
The relationships execute another query like this:
SELECT 'stuff' FROM 'table' WHERE 'foreign_key' = ($parentsId)
So it needs to know the id of the parent (original model). Same logic applies to all relationships in a bit different forms.