Laravel Database Seeders: A Beginner’s Practical Guide
Learn how to use Laravel database seeders and factories to create safe, repeatable sample data for a beginner project.
Laravel database seeders let you add reliable sample data to an app without entering every record by hand. A good beginner workflow is to create a model factory, call it from a seeder, and run that seeder only in a safe local or test environment.
- Use factories to describe reusable sample records and seeders to organize when they are created.
- Start with a small dataset that makes your app easier to build and test.
- Never treat sample passwords or destructive database commands as production-ready.
- Laravel’s official seeding documentation explains the available commands and options.
Why Laravel database seeders are useful
A new application often has an empty database. That makes it hard to check a list page, test search, or see how a layout looks with real-looking content. Adding records by hand takes time and can lead to inconsistent results.
Laravel database seeders provide a repeatable way to insert data. You can create a known set of example users, products, or articles whenever you prepare a development database. This is especially helpful when a teammate needs to run the same project locally.
Seeders are different from migrations. A migration changes the database structure, such as creating a table. A seeder adds or updates records in that structure. Keeping those jobs separate makes your project easier to understand.
If you are learning the framework’s data layer, start with this small feature before building a larger application. You can also review the related guides to Laravel form validation and the Laravel service container.
Set up a small example app
For this example, imagine a simple app that lists books. You need a database table, a model, and a way to create example books. If you already have a Laravel project, use its existing setup instead of creating a second app.
First, generate a model and migration from your terminal:
php artisan make:model Book -m
Open the new migration in database/migrations and add a few useful columns inside the table definition. For instance, a book may have a title and an author name. Keep the first version small; you can add details when the app needs them.
Schema::create('books', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('author');
$table->timestamps();
});
Run the migration against your configured local database:
php artisan migrate
Before running database commands, check the connection in your environment configuration. Beginners often run commands against a database they did not mean to change. Use a local development database while learning.
Create a Laravel database seeder with a factory
A factory describes how to make one model record. Laravel can use factories to generate realistic-looking example values. A seeder then decides how many records to create and which factory to use.
Create a factory for the Book model:
php artisan make:factory BookFactory --model=Book
In the generated factory file, define values for the fields in your table. For a simple example, the built-in Faker helper can generate varied titles and names:
public function definition(): array
{
return [
'title' => fake()->sentence(3),
'author' => fake()->name(),
];
}
Next, create a seeder for books:
php artisan make:seeder BookSeeder
Use the factory inside the seeder’s run method:
public function run(): void
{
Book::factory()->count(10)->create();
}
Make sure the seeder imports the Book model at the top of the file. The create method saves generated records to the database. If you only need temporary model instances and do not want database writes, use the factory’s make method instead.
Run the seeder and check the results
To run only the book seeder, use its class name with Artisan:
php artisan db:seed --class=BookSeeder
Then inspect the records using your preferred database tool, or display them through a simple page. Confirm the titles and authors are present, and check that the number of rows matches what you expected.
When you have several seeders, register them in DatabaseSeeder. This lets you prepare the app’s baseline data with one command:
public function run(): void
{
$this->call([
BookSeeder::class,
]);
}
Then run:
php artisan db:seed
Laravel’s database tools also support rebuilding a local database from migrations and seeders. Read the command options carefully before using reset or refresh commands. Some options drop existing tables and delete their data.
Keep seed data safe and repeatable
A seeder should be easy to run when setting up a development project. It should also be clear about the kind of data it creates. Name seeders for their purpose, such as BookSeeder or CategorySeeder, rather than putting every insert into one long method.
Separate example data from required records
Some records are needed for the app to work, such as a default role or a list of fixed categories. Other records exist only to help you preview pages. Keep these purposes clear in your seeder names and comments.
Factories work well for varied sample records. For fixed records, use explicit values so the result is predictable. Avoid generating data that looks like a real person’s private information.
Protect passwords and production data
Do not put real customer details, personal data, or reused account passwords in seed files. If a demo account is useful, use a clearly fictional account and a safe local-only password. Never assume a sample credential is suitable for a live website.
Most importantly, do not run destructive database commands on a production connection. Check your environment and database name before running commands that reset or refresh tables. For help planning a safe release, see the Laravel deployment security checklist and the Laravel development service.
Common beginner mistakes to avoid
- Forgetting the model import: Add the correct model namespace to the seeder or factory file.
- Using columns that do not exist: Make factory fields match the migration and model.
- Creating too much data: Begin with a small count so local pages stay quick and easy to inspect.
- Expecting every run to behave like a reset: A seeder may add more records each time. Decide whether that is acceptable before rerunning it.
- Mixing schema changes with sample data: Use migrations for table structure and seeders for records.
- Skipping a check after seeding: Confirm the records appear and that the app can read them.
For repeatable tests, consider creating only the records each test needs rather than relying on a large shared dataset. Factories can help produce focused test data, while seeders are useful for setting up a broader local environment.
Frequently asked questions
What is the difference between a Laravel factory and a seeder?
A factory defines how to create model data. A seeder calls factories or inserts fixed records to prepare a database.
How do I run one Laravel seeder?
Run php artisan db:seed --class=BookSeeder, replacing the class name with your seeder’s name.
Will running a seeder delete existing records?
Not by default in the example shown; it creates records. Your own seeder may behave differently, so inspect its code before running it.
Can I use seeders in production?
Sometimes, for carefully planned required data. Do not load development examples or run destructive commands against production.
Where should I put a seeder?
Place it in the project’s database/seeders directory, following the naming and namespace conventions already used by the app.
Build a dependable development workflow
Factories and seeders make it easier to work with a populated app, repeat setup, and test changes without typing every record yourself. Start with one model, keep sample data safe, and check the database target before running commands. Need help planning a Laravel app? Contact Herby Design to discuss your project.
Found this helpful? Share it
New to Laravel? Learn how seeders and factories help create safe, repeatable sample data for a beginner project 🌱#Laravel #PHP #DatabaseSeeders #Factories #WebDevelopment
Caption and hashtags copied: paste them into your post.
Need help with your website?
I build fast, SEO-ready websites and web applications.