
How to create new user without form submission in laravel
Sometimes we need to create a new user or an admin account in our project. In that case, You can use Laravel tinker shell and database seeder to create a new user without form submission. This is an easy and fast way to create any user in laravel without creating any registration form.
-
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')]);
0You 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
0You 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.
Random Code Snippet Queries: Laravel
- Class 'App\Rules\Hash' not found in Laravel
- Print last executed query in laravel
- Insert values in pivot table dynamically in laravel
- How to implement toggleLike() method in Overtrue\LaravelLike laravel package
- How to call controller function from view in Laravel
- Laravel migration add foreign key to existing table
- Insert dummy data in users table Laravel
- How to check email is valid or not in Laravel
- Json encode method in laravel
- Redirect to another view from controller in laravel
- Create project table with model and migration
- Send post data from controller to view
- Attempt to read property "avatar" on null in Laravel
- SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'posts_user_id_foreign'; check that column/key exists
- Print query in laravel
- Laravel order by date not working
- Class 'App\Http\Controllers\User' not found
- Order by multiple columns in Laravel
- Delete file from amazon s3 bucket using Laravel
- How to get file extension from input type file in laravel
- Laravel route redirect not working
- Always load the relationship data with eager loading in Laravel
- How to pass variable from controller to model in Laravel
- Laravel save object to database
- How to get query string value in laravel