我有一个关于 laravel faker 的问题,我正在寻找一个使用 seeders 插入数千条记录的教程
这是我的 PostSeeder.php:
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Post;
use App\Models\User;
class PostSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Post::factory(10)->create();
}
}
这里我是插入10个帖子,但是我需要测试几千或者几百万条记录,所以我看到了一个教程并修改了seeder
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Post;
use App\Models\User;
use Illuminate\Support\Str;
class PostSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$users= collect(User::all()->modelKeys());
$data = [];
for ($i = 0; $i < 100000; $i++) {
$data[] = [
'body' => Str::random(50),
'image' => 'https://via.placeholder.com/640x480.png/0077dd?text=inventore',
'user_id' => $users->random(),
'created_at' => now()->toDateTimeString(),
'updated_at' => now()->toDateTimeString(),
];
}
$chunks = array_chunk($data, 10000);
foreach ($chunks as $chunk) {
Post::insert($chunk);
}
}
}
通过这种方法,我可以更快地插入数千条记录,但问题是我没有正确插入 body 和 image 字段
我想用 faker 尝试一些东西,在我的工厂里我有这个:
PostFactory.php
<?php
namespace Database\Factories;
use App\Models\Post;
use App\Models\User;
use Illuminate\Database\Eloquent\Factories\Factory;
class PostFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Post::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'body' => $this->faker->text,
'image' => $this->faker->imageUrl(),
'user_id' => function() {
return User::factory()->create()->id;
}
];
}
}
我想在 PostSeeder 中使用这些伪造方法,但我不能,我该怎么办?谢谢你。
编辑:
我试过这个:
public function run(Faker $faker)
{
$users= collect(User::all()->modelKeys());
$data = [];
for ($i = 0; $i < 50000; $i++) {
$data[] = [
'content' => $faker->text,
'image_path' => $faker->imageUrl(),
'user_id' => $users->random(),
'created_at' => now()->toDateTimeString(),
'updated_at' => now()->toDateTimeString(),
];
}
$chunks = array_chunk($data, 5000);
foreach ($chunks as $chunk) {
Post::insert($chunk);
}
}
我收到了这条消息: PDOException::("SQLSTATE[HY000]: 一般错误: 2006 MySQL 服务器已经消失") 但是当我尝试使用更少的记录时它起作用了,所以,我像这样更改了播种机:
$users= collect(User::all()->modelKeys());
$posts = Post::factory(10)->create();
$posts = collect($posts->only(['content','image_path']));
...
...
'content' => $posts->random()->content,
'image_path' => $posts->random()->image_path
...
这不起作用,它出现了这个错误: 您申请了 1 件商品,但只有 0 件商品可用。 看起来 $posts->only(['content','image_path']) 不能正常工作。所以我试过这个:
Post::factory(10)->create();
$tweets = Tweet::select(['content','image_path'])->get();
...
'content' => $posts->random()->content,
'image_path' => $posts->random()->image_path
...
它再次适用于一些记录,但是当我尝试使用数千条记录时,我再次收到此错误: PDOException::("SQLSTATE[HY000]: General error: 2006 MySQL server has gone away")
我能做什么?谢谢
由于模型工厂创建内存中的对象,由于内存使用量大,因此不适合大种子。
但是你可以使用
Faker
来生成数据:
use Faker\Generator as Faker;
class PostSeeder extends Seeder
{
public function run(Faker $faker)
{
$users= collect(User::all()->modelKeys());
$data = [];
for ($i = 0; $i < 100000; $i++) {
$data[] = [
'body' => $faker->text,
'image' => $faker->imageUrl(),
'user_id' => $users->random(),
'created_at' => now()->toDateTimeString(),
'updated_at' => now()->toDateTimeString(),
];
}
$chunks = array_chunk($data, 10000);
foreach ($chunks as $chunk) {
Post::insert($chunk);
}
}
}
我个人会做这样的事情:
public function run()
{
$videos = factory(Video::class, 10000)->make();
$chunks = $videos->chunk(2000);
$chunks->each(function ($chunk) {
Video::insert($chunk->toArray());
});
}
通常 快得多,不管 faker 的用法如何