How to create a user from the Tinker console

Overview

In Laravel, you may need to create a user without using a registration form, especially if you are writing tests in your application.

How to create a single user

If we go to the user seeder, we will find this line:

\App\Models\User::factory(10)->create();

This line invokes the User::factory and creates 10 users with fictitious information when we run the database seeders.

However, if we need to create one user and perhaps pass information that we do not want to be false (i.e., the email), we can execute the User::factory line directly from the Tinker console and create the user, as shown below:

User::factory()->create(['email' => 'test-2@mail.com'])

How to create multiple users

You can also provide the count of how many users you want to create with specific credentials. The syntax below works for Laravel 8 and onwards.

User::factory()->count(10)->create(['email' => 'test-2@mail.com'])
New on Educative
Learn any Language for FREE all September 🎉
For the entire month of September, get unlimited access to our entire catalog of beginner coding resources.
🎁 G i v e a w a y
30 Days of Code
Complete Educative’s daily coding challenge every day in September, and win exciting Prizes.

Free Resources