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 < 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);
}
}
}
Answer from Mohsen Nazari on Stack OverflowGitHub
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
Welcm Learning
welcm.uk › blog › getting-started-with-faker-in-laravel
Getting Started With Faker in Laravel | Welcm Learning. Blog
A look at the functions of the Faker PHP library and how to set up Model Factories and Seeders for your Laravel application
Videos
14:25
Laravel Faker | Insert fake data using Faker Factory | Laravel ...
04:53
Faker in Laravel: 10+ Less-Known Methods - YouTube
22:39
How To Use Factory Class and Faker Library To Generate Fake Data ...
03:48
Anonymize Laravel Package: Hide "Sensitive" Data for Demos with ...
05:26
Laravel Faker: Seed Unique Values in Factories - YouTube
21:20
Generar Datos Faker Ficticios en Laravel Usando Factories y Faker ...
FakerPHP
fakerphp.org
FakerPHP / Faker
Faker is a PHP library that generates fake data for you.
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 < 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
Laravel
laravel.com › docs › 7.x › database-testing
Database Testing | Laravel 7.x - The clean stack for Artisans and agents
Instead of manually specifying the value of each column when you create this test data, Laravel allows you to define a default set of attributes for each of your Eloquent models using model factories. To get started, take a look at the database/factories/UserFactory.php file in your application. Out of the box, this file contains one factory definition: ... use Faker\Generator as Faker; use Illuminate\Support\Str; $factory->define(App\User::class, function (Faker $faker) { return [ 'name' => $faker->name, 'email' => $faker->unique()->safeEmail, 'email_verified_at' => now(), 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'remember_token' => Str::random(10), ]; });
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 - Open the LocationsTableSeeder file and add the following code. public function run() { $faker = Faker::create(); foreach (\App\Models\User::all() as $user) { $user->locations()->create([ 'address' => $faker->address, 'latitude' => $faker->latitude, 'longitude' => $faker->longitude, ]); } } After adding the code, execute the seeder file using the following command. ... Completing the above steps adds realistic location data that helps you attach a realistic touch to your testing environment. It might be possible that you cover users all over the world; in that case, you need to test how your system handles data in various languages. Laravel Faker simplifies this process by supporting localization, enabling the generation of data specific to different regions.
Shortcode
shortcode.dev › laravel › faker.html
Laravel cheatsheet > Faker
$faker = Faker\Factory::create(); $faker->name; // First and second name $faker->randomDigit; // A random number $faker->word; // A single word $faker->sentence; // A sentence $faker->unique()->word; // A single unique word $faker->text($maxNbChars = 300); // 300 character long text $faker->safeEmail; // An email address $faker->hexcolor; // Hex color copy
GitHub
github.com › mbezhanov › laravel-faker-provider-collection
GitHub - mbezhanov/laravel-faker-provider-collection: A collection of custom Faker providers for your Laravel applications
A collection of custom Faker providers for your Laravel applications - mbezhanov/laravel-faker-provider-collection
Starred by 21 users
Forked by 6 users
Languages PHP 100.0% | PHP 100.0%
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;
AlexHost
alexhost.com › home › faq › mastering realistic data generation in laravel with faker: a comprehensive guide
Mastering Realistic Data Generation in Laravel with Faker: A Comprehensive Guide
September 30, 2025 - Faker is a powerful PHP library that generates fake, but realistic, data for testing and seeding databases. In Laravel, Faker is integrated with Eloquent model factories, making it easy to create models with random data for development, testing, ...
Laravel Daily
laraveldaily.com › tip › use-faker-outside-factories-or-seeders
Use faker outside factories or seeders
If you want to generate some fake data, you can use Faker even outside factories or seeds, in any class.
Laracasts
laracasts.com › discuss › channels › laravel › laravel-8-using-faker-and-factory
Laravel 8 - Using Faker and Factory
We cannot provide a description for this page right now
Laracasts
laracasts.com › discuss › channels › laravel › using-faker-on-controllers-laravel-8
Using Faker on Controllers - Laravel 8
We cannot provide a description for this page right now
Laravel.io
laravel.io › forum › how-to-create-factory-for-product-and-categories
How to create factory for Product and Categories? | Laravel.io
I think you can use a custom provider if you want to have a custom options for your product and category names: https://fakerphp.github.io/#faker-internals-understanding-providers ... Sign in to participate in this thread! ... The Laravel portal for problem solving, knowledge sharing and community building.
Laravel
laravel.com › docs › 12.x › eloquent-factories
Eloquent: Factories | Laravel 12.x - The clean stack for Artisans and agents
As you can see, in their most basic form, factories are classes that extend Laravel's base factory class and define a definition method. The definition method returns the default set of attribute values that should be applied when creating a model using the factory. 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.