DEV Community

loading...
Cover image for Laravel seeding the database

Laravel seeding the database

dailydevtips1 profile image Chris Bongers Originally published at daily-dev-tips.com ・2 min read

Yesterday we made our first database, and another cool function of Laravel is the option to seed it.

The default installation comes with a basic database/seeders/DatabaseSeeder.

This is a collection place to run specific seeders. For our one, we will create a separate new one.

Creating the Laravel seeder

Let's first generate a new seeder for our newly created books table.

php artisan make:seeder BookSeeder
Enter fullscreen mode Exit fullscreen mode

This will generate the BookSeeder.php file for us. Let's change the run function to create 10 random books for us.

public function run()
    {
    foreach (range(1, 10) as $index) {
        $year = rand(1700, 2020);
        DB::table('books')->insert([
            'title' => Str::random(10),
            'year' => $year,
        ]);
    }
}
Enter fullscreen mode Exit fullscreen mode

To run this function, we need to make one adjustment in our main DatabaseSeeder file.

public function run()
{
    $this->call([
        BookSeeder::class
    ]);
}
Enter fullscreen mode Exit fullscreen mode

And then, we can run it by executing the following command.

php artisan db:seed
Enter fullscreen mode Exit fullscreen mode

This creates 10 random books, as you can see in the image below.

Laravel seeded database

Using the Laravel seeder with a Factory

As you saw above, that is one way of entering our database entries. Still, we can also make use of something called Model factories.
They are generally used when you need to enter large data sets.

Let's create a new factory for our Books model.

php artisan make:factory BookFactory
Enter fullscreen mode Exit fullscreen mode

The Factory, however, works on Models, so let's also create an empty Book model for now.

php artisan make:model Book
Enter fullscreen mode Exit fullscreen mode

Then inside the BookFactory, we can set the model to be the Book model we just created.

use App\Models\Book;

protected $model = Book::class;
Enter fullscreen mode Exit fullscreen mode

And let's add some data for our definition.

public function definition()
{
    return [
        'title' => $this->faker->realText(50),
        'year' => rand(1700, 2020),
    ];
}
Enter fullscreen mode Exit fullscreen mode

This again will just generate some random text for our titles and pick a random year.

Next up, we need to make some changes to our BookSeeder.php file.

Book::factory()
    ->count(10)
    ->create();
Enter fullscreen mode Exit fullscreen mode

This says, use the BookFactory to create 10 entries.

If we now run our seeder again, we should see another 10 random entries.

Laravel seeder with Factory

Pretty cool, right!

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Discussion (0)

pic
Editor guide