How to Use ThinkPHP5 Seeders for Quick Database Mock Data (with Faker)

This guide explains how to create and run a ThinkPHP5 seeder to generate realistic mock data, from a simple mt_rand implementation to a Faker‑based version, including command‑line usage, code examples, and sample database output.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
How to Use ThinkPHP5 Seeders for Quick Database Mock Data (with Faker)

Creating a Seeder in ThinkPHP5

Run the command php think seed:create VideoSeeder to generate a new seeder class file. The generated file includes the namespace import use think\migration\Seeder; and a class definition that extends Seeder with an empty run() method.

Generating Simple Mock Data

Inside the run() method, the example creates an array $rows = []; and fills it with 200 entries using a for loop. Each entry is an associative array with fields 'name', 'email', 'password', 'upload_time', and 'user_id'. Values are generated with mt_rand() for numbers and md5('123456') for the password. After the loop, the data is inserted with: $this->table('video')->insert($rows)->save(); A note reminds you to call save() or the data will not be persisted.

Running the Seeder

Execute the seeder using php think seed:run . The console shows messages such as VideoSeeder: seeding and the total time taken (e.g., 9.1958s ), confirming that all rows have been inserted.

Using Faker for Realistic Data

Install Faker with Composer: <code>composer require fzaninotto/faker</code> Modify the seeder to use Faker: <code>class VideoSeeder extends Seeder { public function run() { $faker = Faker\Factory::create('zh_CN'); $rows = []; for ($i = 0; $i < 100; $i++) { $rows[] = [ 'name' => $faker->name, 'email' => $faker->email, 'password' => $faker->password, 'upload_time' => $faker->unixTime, 'user_id' => rand(1111, 9999), ]; } $this->table('video')->insert($rows)->save(); } } </code> This version generates human‑readable names, email addresses, passwords, timestamps, and user IDs, making test data much more representative of real‑world records. Result After running the Faker‑based seeder, a SELECT * FROM iot_video query returns rows with realistic values, as shown in the sample output tables. The article concludes that using Faker greatly improves the usefulness of seeded data for development and testing.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PHPFakerThinkPHPSeederDatabase Seeding
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.