DEV Community

Peter Fox
Peter Fox

Posted on • Originally published at Medium on

Laravel Tip: Set a seeding seed

Photo by Francesco Gallarotti on Unsplash

Pretty much all apps will eventually have some quite complex seeding that occurs for local development. This is the best way to quickly apply a large amount of data to your database or other data stores.

Sometimes it can be really useful to have seeds to debug issues that you might not expect, especially with UI where it’s hard to make a test to catch out when a name is too long for a UI component.

The problem with that though is that seeding while using Faker is completely random and so reproducing that problem might actually be difficult to do.

Our seeding data doesn’t have to be completely random though, we can in fact apply a seed value which will make sure the data always generates the same data (as long as the code generating the data does not change).

A seed is essentially an initialising value for a random number generator. For the Faker library this allows you to use a string, number or some value that will be a starting point for all random values generated in sequence. This means, using the same seed produces the same result each time. This is until you change the code and generate another value from Faker. You can read more about this in the package’s documentation.

We can apply this method across the board by adding a small snippet of code to the register method of a Service Provider class. In the example we simply used the AppServiceProvider.

Now each time a seed runs, the data will be consistent until we change anything like the factory classes or the seeders themselves. As you can see in the example code we are using an environment variable to set the seed so that way each developer can use their own seed for seeding but can quickly try out someone else’s.

Conclusion

This is all pretty simple stuff but could have a real impact to your team. Give it a go and see if it helps your team debug more issues before they hit production.

I’m Peter Fox, a software developer in the UK who works with Laravel among other things. Thank you for reading my article, I’ve got several more on both medium and dev.to. If you want to know more about me, head over to https://www.peterfox.me. I’m also now also Sponsorable on GitHub. If you’d like to encourage me to write more articles like this please do consider dropping a small one off donation.

Top comments (0)