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.

<?php
namespace App\Providers;
use Faker\Generator;
use Illuminate\Contracts\Container\Container;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
if ($this->app->runningInConsole()) {
$this->app->extend(
Generator::class,
fn (Generator $generator) => tap($generator)->seed(env('SEEDER_SEED'))
);
}
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}

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.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️