Seed multiple rows at once laravel 5
Stefan Izdrail
Founder & Senior Architect · 2026-06-29
Title: Seed Multiple Rows Efficiently in Laravel 5
Body:
Seeding is an essential step during application development to provide initial data for your users, articles, or products. When you need to seed multiple rows of data at once, it's crucial to understand the proper approach that ensures success. In this post, we will learn how to efficiently seed multiple rows in Laravel 5 and fix the code snippet provided.
Understanding Seeding Data in Laravel
Seeding is a process where you can populate the database with test data for testing purposes or production use. In Laravel, you can use a seeder class to execute your seeding logic from the command line. A seeder class must extend the Seeder trait and implement the run() method as shown in the code snippet above.Fixing the Code Snippet
To properly seed multiple rows, we need to optimize how we structure our data in the $users array. In the initial example, you have two arrays containing user information, which is unnecessary for your purpose. Instead, you can combine these arrays into a single array with all users' details as follows:$users = [
['id' => 1, 'name' => 'Stephan de Vries', 'username' => 'stephan', 'email' => 'stephan-v@gmail.com', 'password' => bcrypt('carrotz124')],
['id' => 2, 'name' => 'John doe', 'username' => 'johnny', 'email' => 'johndoe@gmail.com', 'password' => bcrypt('carrotz1243')]
];
User::create($users);
Now, all user data is in a single array, and when you call User::create($users), it will create two users instead of failing.