After you create this seeder, you must run composer dumpautoload, and then in your database/seeds/DatabaseSeeder.php add the following:
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$this->call('UserTableSeeder');
}
}
Now you can finally use php artisan db:seed --class=UserTableSeeder every time you need to insert users in the table.
I think you want to do this once-off, so there is no need for something fancy like creating an Artisan command etc. I would suggest to simply use php artisan tinker (great tool!) and add the following commands per user:
You can also create a new console command which can be called from the command line. This is especially useful if you want to create new users on demand.
This example makes use of laravel fortify but you can also use your own user registration logic.
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Actions\Fortify\CreateNewUser;
class CreateUserCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'user:create {--u|username= : Username of the newly created user.} {--e|email= : E-Mail of the newly created user.}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Manually creates a new laravel user.';
/**
* Execute the console command.
* https://laravel.com/docs/9.x/artisan
*
* @return int
*/
public function handle()
{
// Enter username, if not present via command line option
$name = $this->option('username');
if ($name === null) {
$name = $this->ask('Please enter your username.');
}
// Enter email, if not present via command line option
$email = $this->option('email');
if ($email === null) {
$email = $this->ask('Please enter your E-Mail.');
}
// Always enter password from userinput for more security.
$password = $this->secret('Please enter a new password.');
$password_confirmation = $this->secret('Please confirm the password');
// Prepare input for the fortify user creation action
$input = [
'name' => $name,
'email' => $email,
'password' => $password,
'password_confirmation' => $password_confirmation
];
try {
// Use fortify to create a new user.
$new_user_action = new CreateNewUser();
$user = $new_user_action->create($input);
}
catch (\Exception $e) {
$this->error($e->getMessage());
return;
}
// Success message
$this->info('User created successfully!');
$this->info('New user id: ' . $user->id);
}
}