Home > Enterprise >  Database not seeding with proper factories and seeder class in Laravel
Database not seeding with proper factories and seeder class in Laravel

Time:04-23

The Problem is my database not seeded with php artisan db:seed in my laravel project. no errors showing and after the command my database just empty but php artisan db:seed showing success result.

This is my run method in DatabaseSeeder.php file under database/seeders folder in my laravel project.

$this->call([
    ItemBrandSeeder::class,
    ItemDepartmentSeeder::class,
    ItemUnitSeeder::class,
    CategorySeeder::class,
    ItemSeeder::class
]);

This is my run method in ItemBrandSeeder.php under database/seeders folder in my laravel project.

ItemBrandFactory::times(100);

This is my definition method in ItemBrandFactory.php under database/factories folder in my laravel project.

return [
    'stvalue1' => $this->faker->randomElement(['BOX', 'PCS', 'NOS', 'PACK']),       
    'stdescription' => $this->faker->text('30'),
    'stvalue2' => $this->faker->randomElement(['Countable', 'Measurable']),
    'stflag' => "UNIT"
];

This is my run method in ItemDepartmentSeeder.php under database/seeders folder in my laravel project.

ItemDepartmentFactory::times(25);

This is my definition method in ItemDepartmentFactory.php under database/factories folder in my laravel project.

return [
    'stCategory' => $this->faker->text(25),
    'sttype' => ['department']
];

This is my run method in ItemUnitSeeder.php under database/seeders folder in my laravel project.

DB::connection("dynamic_db")->table('tblgeneral')->insert([
    [
        'stflag' => 'UNIT',
        'stdescription' => "Pieces",
        'stvalue1' => "PCS",
        'stvalue2' => "Countable"
    ],

    [
        'stflag' => 'UNIT',
        'stdescription' => "Box",
        'stvalue1' => "BOX",
        'stvalue2' => "Countable"
    ],

    [
        'stflag' => 'UNIT',
        'stdescription' => "Bar",
        'stvalue1' => "BAR",
        'stvalue2' => "Measurable"
    ],

    [
        'stflag' => 'UNIT',
        'stdescription' => "Nos",
        'stvalue1' => "NOS",
        'stvalue2' => "Measurable"
    ],

    [
        'stflag' => 'UNIT',
        'stdescription' => "Feet",
        'stvalue1' => "FT",
        'stvalue2' => "Measurable"
    ],
]);

There is no factory class for ItemUnitSeeder This is my run method in CategorySeeder.php under database/seeders folder in my laravel project.

CategoryFactory::times(50);

This is my definition method in CategoryFactory.php under database/factories folder in my laravel project.

return [
    'stCategory' => $this->faker->text(25),
    'sttype' => ['category']
];

This is my run method in ItemSeeder.php under database/seeders folder in my laravel project.

ItemFactory::times(1000);

This is my definition method in ItemFactory.php under database/factories folder in my laravel project.

$edate = array(\Carbon\Carbon::create('1973-09-17 04:30:43'), \Carbon\Carbon::now())[rand(0, 1)];
$unit2 = array('na', 1, 2, 3, 4, 5)[rand(0, 5)];
$purchasePrice = $this->faker->numberBetween(200, 10000);
$retailPrice = $purchasePrice   ($this->faker->numberBetween(5, 40) / 100 * $purchasePrice);
$minPrice = $retailPrice   $this->faker->numberBetween(0, 50);
$supplierGivenDiscount = $this->faker->numberBetween(0, 10);
$retailDiscount = $this->faker->numberBetween(0, 5);
$unit2Def = $this->faker->numberBetween(2, 12);
$unit2Price = ($retailPrice / $unit2Def) / 100 * rand(1, 50);
$wholesalePrice = $retailPrice - $this->faker->numberBetween(0, 5) / 100 * $retailPrice;
$wholesaleDiscount = $wholesalePrice - $this->faker->numberBetween(0, 3) / 100 * $wholesalePrice;

return [
    'stItemCode' => getSerialNo('item'),
    'stDescription' => $this->faker->text(100),
    'stCategory' => rand(0, 50),
    'stUnit1' => rand(0, 5),
    'stBrand' => rand(0, 100),
    'stDepartment' => rand(0, 25),
    'stDescription2' => $this->faker->text(200),
    'daEdate' => $edate,
    'tiContaminable' => $edate->eq(\Carbon\Carbon::create('1973-09-17 04:30:43')),
    'dbMinPrice' => $minPrice,
    'dbPurchasePrice' => $purchasePrice,
    'dbSupplierGivenDiscount' => $supplierGivenDiscount,
    'dbRetailPrice' => $retailPrice,
    'dbRetailPriceDiscount' => $retailDiscount,
    'stShelf' => rand(0, 30),
    'siItemType' => rand(0, 3),
    'stUnit2' => $unit2,
    'dbUnit2Price' => $unit2 != 'na' ? $unit2Price : 0,
    'dbUnit2Def' => $unit2 != 'na' ? $unit2Def : 0,
    'dbWholeSalesPrice' => $wholesalePrice,
    'dbWholeSalesPriceDiscount' => $wholesaleDiscount,
    'dbReorderLevel' => 0,
    'stBranch' => Branch::all()->toArray()[rand(0, Branch::count())]['id'],
    'stWebUpdateStatus' => 1,
];

Command and output in console

Command and output in console

But in my database no data are seeded my database just empty.

In case if your wonder why am I used DB class to create ItemUnits in ItemUnitSeeder.php file its because I only wanted 5 units so I thought making a Factory for that useless.

Category factory, Item brand factory and Item department factory using same table name called "tblCategory" only different is "sttype" field. if "sttype" is set to "category" than it's category and if it is set to "brand" than its brand and so on.

CodePudding user response:

Your code is correct and there is no problem, but I think your problem is here: Instead of using times() Use a factor like this: Item::factory()->count(1000)->create();

  • Related