Today just asked myself if i can change user password in Laravel via command line, so things will be easier for us to work with Laravel products. I bet many of you use default Laravel user model (which is good for us in this case). if you do, this article is for you, i found an interesting tool within Laravel command line interface name ‘tinker’
if you use Ruby on Rails, you can think of Laravel tinker tool as something like `rails console` command , it allows you to interact with your application, you can access directly to models, helpers, classes …etc… Have you started liking it yet ? 😉
ok, it’s time to get to main purpose of this article – to change user password with tinker tool – a brief introduction to tinker tool . i will make it step-by-step
Step 1 – start laravel tinker tool
cd <your_larave_project_directory_path> php artisan tinker Psy Shell v0.4.4 (PHP 5.5.28 — cli) by Justin Hileman >>>
now you can start typing whatever you like beind >>> prommpt.
Step 2 – Access to User Model
>>> $user = App\User::where('email', 'nd.thanh@outlook.com')->first(); => <App\User #0000000051a8ab8a000000014bcb4fe8> { id: 342, name: "Thanh, nguyen", email: "nd.thanh@outlook.com", created_at: "2015-09-21 19:28:14", updated_at: "2015-09-23 07:48:14", company: null, api_key: null, cb_user_id: "", firstname: null, lastname: null, timezone: null, last_activity: null, enabled: null, avatar: "", identifier: "", admin_hash: "admin:9569a4dc201b26a01f19dd7594843da1", group_id: 0, role_id: 0 }
so this is the query `App\User::where(’email’, ‘nd.thanh@outlook.com’)->first();` , we have an user instance for my user account now. let’s update password for this user account
Step 3 – update password
Laravel uses Bcrypt to make password hash by default, which means you can generate a password like string with this command
>>> echo Hash::make('yourpassword'); $2y$10$AR.gZdnx6rc9NLLtnuoPzOpOy3D/NZ.GqhFAl0lO0EgJ8boyqX8yK => null
ok, we know how to make password , time to update user instance
>>> $user->password = Hash::make('thanhnd123'); => "$2y$10$At6GpoCsB.BQOacms87fAubGRMtu2UqJ44f53IN2EdT43IGDI.5oO" >>> $user->save() => true
that’s it. my new password is now ‘thanhnd123’ , you can change yours to what ever you like.