by Devin Yang
(This article was automatically translated.)

Published - 7 years ago ( Updated - 7 years ago )

In Laravel, how to use ModelFactory to create Chinese fake data?
I did this for reference.
define(AppUser::class, function (FakerGenerator $faker) {
static $password;
$tw_faker = TwFacker::create('zh_TW');
return [
'name' => $tw_faker->name,
'address' => $tw_faker->address,
'city' => preg_replace('#(.{3})(.*)#um', '$1', $tw_faker->city),
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => str_random(10),
];
});

Customize a TwFacker alias, we can use him.
Here, $tw_faker->name is used to replace the original $faker->name provided by $faker->name , so the Chinese name can be stored.

We can also adjust relevant localized files in vendor/fzaninotto/faker/src/Faker/Provider/zh_TW in Laravel's special security directory.

For example, the text of Text.php can generate fake data Chinese that is more in line with our needs..
https://github.com/fzaninotto/Faker/blob/master/src/Faker/Provider/zh_TW/Text.php

Tags: laravel ModelFactory

Devin Yang

Feel free to ask me, if you don't get it.:)

No Comment

Post your comment

Login is required to leave comments

Similar Stories


dlaravel

D-Laravel v1.5.0 is a new version.

1. Change the image generation method of fpm, optimize dockerfiles/fpm/php7.x/Dockerfiles, and make it easier to set and adjust extension packages. 2. Adjust docker-compose-build.yml, and in the Project directory of D-Laravel, You can quickly build your own image through ./console build. And many more changes....

dlaravel

Use D-Laravel to build your own dedicated php fpm image.

D-Laravel has provided a built php image, if you need to adjust it yourself and build your own dedicated image is quite simple. 1. First, enter dockerfiles/fpm in D-Laravel, and select the PHP version you want to build, such as 7.2. The command is as follows...

laravel

Random passwords in Laravel maintenance mode

The functions mentioned in this article can only be used for Laravel 8 and later versions. If we have some test websites outside Laravel with external IPs, but we only want to access them for ourselves. Here's how I did it for reference