w3resource

Laravel Tutorial (5.7) Seeding

Introduction

Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory. Seed classes may have any name you wish, but probably should follow some sensible convention, such as UsersTableSeeder, etc. By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, allowing you to control the seeding order.

To get started, let's assume we have 2 tables users and products. Our 'users' table contains columns name, email, password. And our 'products' table has columns name, description.

Open the terminal in your project root directory and run the below 2 commands one after another.

php artisan make:seeder UsersTableSeeder php artisan make:seeder ProductsTableSeeder Above commands will create 2 files UsersTableSeeder.php and ProductsTableSeeder.php under the database/seeds folder. We are inserting 50 records each in both tables. For that, in the run function of both seeders, we need to write a code for it.

UsersTableSeeder.php

<?php
 
use Illuminate\Database\Seeder;
 
class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\User::class, 50)->create()->each(function ($u) {
            $u->save();
        });
    }
}

ProductsTableSeeder.php

<?php
 
use Illuminate\Database\Seeder;
 
class ProductsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        factory(App\Product::class, 50)->create()->each(function ($p) {
            $p->save();
        });
    }
}

Writing Factories

Next, we need to write factories for our Models. Open the file ModelFactory.php from the database/factories place the below code in it.

ModelFactory.php

<?php
 
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| Here you may define all of your model factories. Model factories give
| you a convenient way to create models for testing and seeding your
| database. Just tell the factory how a default model should look.
|
*/
 
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\User::class, function (Faker\Generator $faker) {
    static $password;
 
    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('secret'),
        'remember_token' => str_random(10),
    ];
});
 
$factory->define(App\Product::class, function (Faker\Generator $faker) {
 
    return [
        'name' => $faker->word,
        'decsription' => $faker->sentence,
    ];
});

In the above code, we have passed the values using the instance of Faker\Generator to our table columns.

Run The Seeder

Now we all set with our factories and seeder classes. Only thing remains is run the seeder. For doing so, we need to give a call to our seeder classes.

Open DatabaseSeeder.php from the database/seeds directory and write the below code under the run() method.

DatabaseSeeder.php

<?php
 
use Illuminate\Database\Seeder;
 
class DatabaseSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $this->call(UsersTableSeeder::class);
        $this->call(ProductsTableSeeder::class);
    }
}


Next, in the terminal run the command php artisan db:seed. This command starts execution and inserts the fake data in your specified database tables.

Previous: Laravel Tutorial (5.7) Redis
Next: Laravel (5.7) Tutorial



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/laravel/seeding.php