laravel seeder multiple inserts parameters 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:
<?php
use Faker\Generator as Faker;
$factory->define(\App\Product::class, function (Faker $faker) {
return [
'name' => $faker->name,
'price' => $faker->randomFloat(2, 0, 8),
'description' => $faker->text
];
});
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class ProductTableSeeder extends Seeder
{
public function run()
{
\App\Models\Product::factory(10)->create();
}
}
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(ProductTableSeeder::class);
}
}
$php artisan db:seed
$php artisan tinker
>>>App\Models\Product::all()
>>> DB::table('products')->get();