Multilingual E-commerce API using Laravel 8.0 – Part 4 Database Seeder and Factories

One of the cool Laravel feature is its database seeding functionality and generating factories. This can be used to add some sample data to your website or can be used in integration testing.

We will be using Laravel seeders to add data to our API database.

All the migrations go inside the database/seeds folder in your project root directory

#Step 1

Lets create a first seeder using the following command.

php artisan make:seeder CategorySeeder

The above command should create a new file CategorySeeder.php under the database/seeds directory.

Lets add a single category in the CategorySeeder.php file. For now lets hardcode it. We will revisit this seeder to generate multiple categories later in this tutorial.

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $category = new Category();
        $category->category_name = "Accessories";
        $category->category_description = "Accessories";
        $category->save();
    }

Lets also create another seeder for the language table using the seeder command.

php artisan make:seeder LanguageSeeder

Now, lets add some languages in the LanguageSeeder.php file.

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $language = new Language();
        $language->language_name = "English";
        $language->language_code = "en-gb";
        $language->save();

        $language = new Language();
        $language->language_name = "French";
        $language->language_code = "fr-ch";
        $language->save();
    }

#Step 2

We will also create factories for the product and the productTranslations database tables.

To generate a factory use the following command below.

php artisan make:factory ProductFactory --model=product

The above command will generate a file ProductFactory.php in database/factories directory.

<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\model\Product;
use Faker\Generator as Faker;

$factory->define(Product::class, function (Faker $faker) {
    return [
        'product_price' => $faker->numberBetween(10,20),
        'category_id' => 1
    ];
});

Similarly, we will create another factory for the ProductTranslations table.

php artisan make:factory ProductTranslationsFactory --model=productTranslations
<?php

/** @var \Illuminate\Database\Eloquent\Factory $factory */

use App\model\ProductTranslations;
use Faker\Generator as Faker;

$factory->define(ProductTranslations::class, function (Faker $faker) {
    return [
        'product_name' => $faker->word,
        'product_description' => $faker->paragraph,
        'product_id' => $faker->unique()->numberBetween(1, App\model\Product::count()),
        'language_id' => 1
    ];
});

#Step 3

Include the seeders and the generated factories in the database seeder file. The main database seeder file is located in database/seeds/DatabaseSeeder.php

In the run() method add the seeders and factories

    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        // $this->call(UserSeeder::class);
        $this->call(LanguageSeeder::class);
        $this->call(CategorySeeder::class);
        Factory(\App\model\Product::class, 20)->create();
        Factory(\App\model\ProductTranslations::class, 20)->create();
    }

#Step 4

Run the seeders.

php artisan db:seed

Series Navigation<< Multilingual E-commerce API using Laravel 8.0 – Part 3 Product ControllerMultilingual E-commerce API using Laravel 8.0 – Integration Testing Setup >>