Introduction to DB seeds

Database seeding is a set of processes for populating a database with placeholder testing data. This data can be used for testing, development, or providing a baseline for the application.

In the section below, we’ll discuss database seed usage in web applications.

Consistent testing environment

During software development, in-depth testing can be invaluable for finding bugs and potential inconsistencies in the implementation of the logic. By utilizing database seeds, developers can create a consistent and reliable testing environment. This ensures that every time tests are run, the database contains the same set of initial data, reducing the variability of test results and making it easier to reproduce and diagnose issues.

Deployment configuration

At the time of deployment, a system can go through many different stages, such as development, staging, and production. Database seeds allow developers to set up the initial placeholder data for these environments without the need to manually recreate it every time. This enables developers to simplify the deployment process and reduces the chance of discrepancies between different environments.

Ease of demonstrations and showcasing

While demonstrating or showcasing developed applications to primary stakeholders, it can be helpful to have a populated database with sample data. Seeding can be helpful for an engaging and realistic experience for the viewer, especially for applications that involve data visualization, analytics, or content-driven features.

Onboarding and collaboration

Software development is a continuously evolving process. A new developer who joins the project is required to understand it. The seeding of well-defined and populated initial data can significantly speed up their onboarding process. It provides them with a clear understanding of the data structure and relationships within the application. It also facilitates collaboration among team members because everyone works with a shared baseline of data.

Version control and rollbacks

Analyzing the functionality of data through testing can offer insights into how it operates. Usually, placeholder testing data is maintained alongside the software codebase. As the software advances and new features are introduced, any modifications made to the data can also be tracked. If there’s a need to revert to a previous version or rollback, it becomes possible to restore the database to its previous state along with the codebase.

Seeds in Laravel

All the seeds in Laravel are stored in database/seeders with a .php extension. The Artisan command line tool of Laravel provides a convenient way to create seeds.

In the code snippet below, we create a seeder named usersDummy.

Get hands-on with 1400+ tech skills courses.