Testing your application with a large dataset is essential to ensure it performs well under real-world conditions. Thankfully, Laravel provides an easy way to seed your database with realistic data using model factories.

In this blog, you’ll learn how to efficiently seed a Laravel database with large datasets, speeding up the process and optimizing performance.

Using the create method

When using the create method in Laravel, it runs individual insert queries for each record, which can be very slow if you want to insert thousands of records -

User::factory(10000)->create();

The above will create 10,000 users by running 10,000 database insert queries.

Using the insert method

Use insert method in combination with PHP’s array_chunk method to insert records in bulk as shown in below example.

$users = User::factory(10000)->make()->toArray();

foreach (array_chunk($users, 1000) as $chunk) {
	User::insert($chunk);
}

The above code will run a single insert query for each chunk of 1000 records.

INSERT INTO "users" (name, email, address, phone)
VALUES 
(
	"Carl", "carl@example.com", "Main Street", "+61 782-112-123"
),
(
	"Adam", "adam@example.com", "King Street", "+41 423-11-1213"
)
...

The toArray method returns the array representation of the model. If you have hidden attributes in your model those will not be present in the returned array. To fix this issue you can use the makeVisible method to make them visible temporarily

$users = User::factory(10000)
            ->make()
            ->makeVisible(['password', 'remember_token'])
            ->toArray();

Handling memory limit issues

During seeding a large dataset you may encounter the memory limit exceed error, try increasing memory limit for the seeder script by using the ini_set method -

ini_set("memory_limit", "256M");

Using the above method can significantly improve the seeding process of your app.