Laravel
laravel.com › docs › 7.x › database-testing
Database Testing | Laravel 7.x - The clean stack for Artisans and agents
1$factory->define(App\Admin::class, function (Faker\Generator $faker) {
FakerPHP
fakerphp.org
FakerPHP / Faker
Each call to $faker->name() yields a different (random) result.
Videos
06:09
Creating fake data in Laravel using factories and faker - Laravel ...
17:12
How to use Faker in Laravel for Database Seeding - YouTube
09:44
How to use Faker with Laravel Tutorial - YouTube
12:09
Laravel 8 Database Seeding and Faker - YouTube
08:11
Laravel 5.8 - Factorías y Faker [Tutorial en Español 2019] - YouTube
08:00
Laravel Factories: Generate and Re-use Fake Records - YouTube
Welcm Learning
welcm.uk › blog › getting-started-with-faker-in-laravel
Getting Started With Faker in Laravel | Welcm Learning. Blog
We can create localised fake data simply by specifying the locale when creating the Faker, for example: ... Since Laravel 5.2 the locale can also be configured in your /config/app.php file, find the faker_locale section and update, for example:
InterServer
interserver.net › home › mastering realistic data generation in laravel with faker: a comprehensive guide
Mastering Realistic Data Generation in Laravel with Faker: A Comprehensive Guide - Interserver Tips
January 28, 2024 - To generate data with a complex ... customized data structures, providing flexibility in generating complex data. Laravel Faker helps infuse realistic location data....
GitHub
github.com › fzaninotto › Faker
GitHub - fzaninotto/Faker: Faker is a PHP library that generates fake data for you · GitHub
pattern-lab/plugin-faker: Pattern Lab is a Styleguide, Component Library, and Prototyping tool. This creates unique content each time Pattern Lab is generated. guidocella/eloquent-populator: Adapter for Laravel's Eloquent ORM.
Starred by 26.7K users
Forked by 3.6K users
Languages PHP
Laravel
laravel.com › docs › 12.x › eloquent-factories
Eloquent: Factories | Laravel 12.x - The clean stack for Artisans and agents
Via the fake helper, factories have access to the Faker PHP library, which allows you to conveniently generate various kinds of random data for testing and seeding.
Shortcode
shortcode.dev › laravel › faker.html
Laravel cheatsheet > Faker
Faker function lets you generate dummy data for seeding the database. #laravel#db
Faker
faker.readthedocs.io
Welcome to Faker’s documentation! — Faker 40.12.0 documentation
Welcome to Faker’s documentation!
Laravel
laravel.com › docs › 8.x › database-testing
Database Testing | Laravel 8.x - The clean stack for Artisans and agents
Via the faker property, factories have access to the Faker PHP library, which allows you to conveniently generate various kinds of random data for testing.
DEV Community
dev.to › victormlima98 › extending-php-faker-library-to-define-custom-data-structures-using-laravel-11-25lc
Extending PHP Faker Library to define custom data structures using Laravel 11 - DEV Community
June 7, 2024 - ':' . config('app.faker_locale'), concrete: Generator::class ); } } Then we have to register the Provider in our application, in bootstrap/providers.php: return [ App\Providers\AppServiceProvider::class, App\Providers\TestServiceProvider::class, ]; As you can see, we registered a binding in the Service Container telling Laravel that it must add our custom class as a Faker provider, so now Faker is aware of the methods we've just created.
GitHub
github.com › teepluss › laravel-faker
GitHub - teepluss/laravel-faker: Faker is a PHP library that generates fake data for you · GitHub
Faker is a PHP library that generates fake data for you. Whether you need to bootstrap your database, create good-looking XML documents, fill-in your persistence to stress test it, or anonymize data taken from a production service, Faker is for you.
Author teepluss
Sven Hofmann
hofmannsven.com › 2021 › faker-provider-in-laravel
How to implement a custom Faker Provider in Laravel | Sven Hofmann
March 24, 2021 - Next, register the class in Laravel. You can generate a new Laravel service provider via the artisan command: ... use App\Faker\FrameworkProvider; use Faker\{Factory, Generator}; use Illuminate\Support\ServiceProvider;
Packagist
packagist.org › packages › fakerphp › faker
fakerphp/faker - Packagist.org
Each call to $faker->name() yields a different (random) result.
Laravel
laravel.com › docs › 11.x › eloquent-factories
Eloquent: Factories - Laravel 11.x - The PHP Framework For Web Artisans
Via the fake helper, factories have access to the Faker PHP library, which allows you to conveniently generate various kinds of random data for testing and seeding.
ZetCode
zetcode.com › php › faker
PHP Faker - generating fake data in PHP with Faker package
February 16, 2025 - PHP Faker tutorial shows how to generate fake data in PHP with Faker package.
Top answer 1 of 6
38
Looking at the documentation and an issue raised on the their Github issues section, your solution seems to be the best. Some methods allow you to specify the gender for a name so you could do like this:
$gender = $faker->randomElement(['male', 'female']);
return [
'name' => $faker->name($gender),
'email' => $faker->safeEmail,
'username' => $faker->userName,
'phone' => $faker->phoneNumber,
'gender' => $gender,
'address' => $faker->address,
'dob' => $faker->date($format = 'Y-m-d', $max = 'now'),
'password' => bcrypt('secret')
];
Hopefully this fits your requirement.
2 of 6
4
to do it without additional variable, do it like this
return [
'gender' => $faker->randomElements(['male', 'female']),
'name' => $faker->name(function (array $user) {return $user['gender'];})
]
hope it helps
Top answer 1 of 2
3
Since model factories create in-memory objects, because of big memory usages it is not suitable for large seeds.
But you can use Faker for data generation:
use Faker\Generator as Faker;
class PostSeeder extends Seeder
{
public function run(Faker $faker)
{
$users= collect(User::all()->modelKeys());
$data = [];
for ($i = 0; $i < 100000; $i++) {
$data[] = [
'body' => $faker->text,
'image' => $faker->imageUrl(),
'user_id' => $users->random(),
'created_at' => now()->toDateTimeString(),
'updated_at' => now()->toDateTimeString(),
];
}
$chunks = array_chunk($data, 10000);
foreach ($chunks as $chunk) {
Post::insert($chunk);
}
}
}
2 of 2
0
Personally I would do something like this:
public function run()
{
$videos = factory(Video::class, 10000)->make();
$chunks = $videos->chunk(2000);
$chunks->each(function ($chunk) {
Video::insert($chunk->toArray());
});
}
Generally much faster, regardless of faker usage