How To Generate Test Users in Laravel 10 Tutorial

Reading Time: 3 minutes
122 Views

Populating a Laravel application with test users is crucial for testing functionalities, assessing performance, and simulating real-world scenarios during development or quality assurance phases. In Laravel 10, creating test users involves generating mock user data to validate application features and interactions.

In this tutorial, we’ll see the easiest and comprehensive process of generating test users in Laravel 10. This functionality empowers developers to automate the creation of test user accounts with synthetic data, facilitating efficient and reliable testing of various application functionalities.

Read More: How to Paginate Laravel 10 API Resources Tutorial

Let’s get started.

Laravel Installation

Open terminal and run this command to create a laravel project.

composer create-project laravel/laravel myblog

It will create a project folder with name myblog inside your local system.

To start the development server of laravel –

php artisan serve

URL: http://127.0.0.1:8000

Assuming laravel already installed inside your system.

Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Read More: How To Use Laravel 10 Queue To Send Emails Tutorial

Setup “users”, The Database Table

When you install the setup of laravel, you get migrations to create “users”, “password_resets”, etc database table.

So, after database connectivity you don’t need to do anything inside application.

Open project terminal and run this command,

php artisan migrate

You must see all migrations will be migrated and tables will be created inside your database.

Now, we have “users” table.

Generate Test Data for “users” Table

In your application setup you will see a file called UserFactory.php inside database/factories folder.

Here, we will see two methods by which we will generate test users for table.

Method #1: Generate Users Using DatabaseSeeder

Open DatabaseSeeder.php file,

Remove comment lines from this line of code,

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

Next, go to project terminal and run this command

php artisan db:seed

Successfully, you have 100 test users in your users table.

Read More: Build WordPress User Login Using Laravel Corcel Tutorial

Method #2: Generate Users Using Artisan Tinker

Go to project terminal and run this command,

php artisan tinker

It will open a shell command panel where you have to execute this,

> App\Models\User::factory(100)->create()

You will get 100 test users in your users table.

Seed-Test-Users-in-laravel-Database

That’s it.

We hope this article helped you to learn about How To Generate Test Users in Laravel 10 Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more