laravel seed multiple records with factory code example
Example: laravel seed multiple records with factory
$ php artisan make:seeder ProductTableSeeder
//Creates a ProductTableSeeder file on Database/seeders
$php artisan make:Factory ProductFactory
//Creates a ProductTableSeeder file on Database/seeders
$php artisan make:model Product
//Go to database/Factories/ProductFactory and paste:
define(\App\Product::class, function (Faker $faker) {
return [
'name' => $faker->name,
'price' => $faker->randomFloat(2, 0, 8),
'description' => $faker->text
];
});
//Go to database/seeders/ProductTableSeeder and paste:
create();
}
}
//Go to database/seeders/DatabaseSeeder and paste:
call(ProductTableSeeder::class);
}
}
//Finally run:
$php artisan db:seed
$php artisan tinker
//Check the data inserted on the model product.
>>>App\Models\Product::all()
//Alternativelly you can also run:
>>> DB::table('products')->get();