插入千条记录时在 laravel 中使用 faker

Use faker in laravel when inserting thousand of records

我有一个关于 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]: 一般错误: 2006 MySQL 服务器已经消失")

我能做什么?谢谢

由于模型工厂在内存中创建对象,因此内存使用量大,因此不适合大种子。

但您可以使用 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);
        }
    }
}