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 to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources