
Laravel create default admin user
Laravel create default admin user
With this article, we’ll look at some examples of how to address the "Laravel create default admin user" problem.
Sometimes you have to create an admin account in a laravel application. You can create an account using register form in laravel or you can also create the account using seeder and tinker shell. You can also create a key value pair for extra column which exists in your users table-
Create user using php artisan tinker command
//Run tinker shell using tinker command on terminal php artisan tinker //Just copy paste below code in tinker shell it will create a new user for you. //Create user using create method on user model User::create(['name'=>'admin', 'email'=>'[email protected]', 'email_verified_at'=> now(), 'password'=> bcrypt('adminpass')]);
You can create a user using the tinker command in Laravel. You have to run the tinker command php artisan tinker to start the tinker shell after that you create a user using the create method on the user model. You can also pass additional columns and values in the script such as ‘role_id’=> 2.
-
Create user using seeder
// Create Seeder file using command php artisan make:seeder AdminUserSeeder //database\seeders\AdminUserSeeder.php <?php namespace Database\Seeders; use Illuminate\Database\Seeder; use App\Models\User; class AdminUserSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { User::create(['name'=>'admin', 'email'=>'[email protected]', 'email_verified_at'=> now(), 'password'=> bcrypt('adminpass')]); } } //database\seeders\DatabaseSeeder.php public function run() { $this->call([ AdminUserSeeder::class, ]); } //Command for database seeding php artisan db:seed --class=AdminUserSeeder
You can create a new user using a database seeder in laravel. You have to create a seeder using php artisan make:seeder command. After that you can use your user create script in the run method in the created file under database\seeders directory, pass seeder class to DatabaseSeeder.php and run php artisan db:seed command.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Call to undefined method Illuminate\Support\Facades\Request::all()
- The POST method is not supported for this route. Supported methods: PUT.
- How to call controller function from view in Laravel
- Print query in laravel
- How to get user information using hootlex/laravel-friendships package in laravel
- How to get records in random order in laravel
- Display data in table using foreach in Laravel
- How to call model in blade laravel
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- How to insert value to additional columns in pivot table in laravel
- Create record with unique slug in laravel
- Send OTP using textlocal api in laravel
- How to check column value of a record is null or not in laravel
- Link storage folder in laravel 8
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement
- Laravel delete all rows older than 30 days
- Show old value while editing the form in Laravel
- Class App\Http\Controllers\Admin\UserController Does Not Exist
- How to get file extension from input type file in laravel
- Call to undefined method App\Models\User::follow()
- Symlink(): No such file or directory
- Validation errors for multiple forms on same page Laravel
- How to get the id of last record from collection object in laravel view
- Global scope in Laravel with example
- Validation for multiple forms on same page in laravel