php Laravel Faker工厂关系[duplicate]

ffdz8vbo  于 2023-05-05  发布在  PHP
关注(0)|答案(6)|浏览(179)

此问题已在此处有答案

Laravel 8 Multiple Relationships for Factory(6个回答)
10小时前关闭。
我有两个工厂,一个负责分类,另一个负责产品。当我运行工厂时,我想为生成的每个类别创建x个产品。我该怎么写代码来生成这个呢
类别的定义如下所示:

return [
            'name' => $this->faker->word,
            'slug' => Str::slug($this->faker->unique()->word, '-'),
        ];

产品的定义如下:

return [
                'category_id' => 1, //instead of 1 the category id used i want to be random
                'name' => $this->faker->word,
                'slug' => Str::slug($this->faker->unique()->word, '-'),
                'description' => $this->faker->paragraph,
                'price' => $this->faker->randomFloat(2, 0, 10000),
                'is_visible' => 1,
                'is_featured' => 1
            ];

正如您所看到的,我硬编码了category_id,我不太确定如何让它自动生成并创建每个类别的产品。我有工厂的类别写的这样,以创建10个项目

Category::factory()
                ->count(10)
                ->create();

我尝试了这个试错,以为它会工作,但我得到一个错误,category_id cannot be null

Product::factory()
                ->has(Category::factory()->count(2))
                ->count(20)
                ->create();
mwyxok5s

mwyxok5s1#

$factory->define(Product::class, function (Faker $faker) {
    return [
        'category_id' => factory(Category::class), //instead of 1 the category id used i want to be random
        'name' => $this->faker->word,
        'slug' => Str::slug($this->faker->unique()->word, '-'),
        'description' => $this->faker->paragraph,
        'price' => $this->faker->randomFloat(2, 0, 10000),
        'is_visible' => 1,
        'is_featured' => 1
    ];
});

有关详细信息,请参阅此复制报价的来源:
通过将该属性设置为factory()的示例,Laravel也会延迟地创建该模型并自动关联它
-- https://www.dwightwatson.com/posts/building-relations-with-laravel-model-factories

lyfkaqu1

lyfkaqu12#

您只需将CategoryFactory传递给category_id

return [
    'category_id' => Category::factory(),
    // ...
];

您可以在这里阅读更多关于工厂的信息:https://laravel.com/docs/8.x/database-testing#defining-relationships-within-factories

wdebmtf2

wdebmtf23#

如果你想为每个创建的类别创建多个产品,你可以这样做:

// CategoryProductSeeder
$categories = Category::factory(50)->create();

$categories->each(function ($category) {
    $categories->products()->saveMany(
        Product::factory(10)->make()
    );
});
yzxexxkh

yzxexxkh4#

这是我的工作,因为我使用Laravel 8。
产品定义:

return [
            'category_id' => Category::factory(),
            'name' => $this->faker->word,
            'slug' => Str::slug($this->faker->unique()->word, '-'),
            'description' => $this->faker->paragraph,
            'price' => $this->faker->randomFloat(2, 0, 1000),
            'is_visible' => 1,
            'is_featured' => 1
        ];

播种机:

Product::factory()
                ->has(Category::factory())->count(50)
                ->create();

创建了50个类别和50个产品。为每个产品分配1个类别。

dzjeubhm

dzjeubhm5#

use Illuminate\Support\Str;

public function definition(): array
{
    return [
        'category_id'=>'1',
        'title'=> $this->faker->text(20),
        'slug'  => function ($attr) {
            return Str::slug($attr['title']);
        },
        'content'=> $this->faker->realText($maxNbChars = 200, $indexSize = 2),
        'is_published'=> $this->faker->boolean()
    ];
}
6jjcrrmo

6jjcrrmo6#

我使用了不同的语法,但我认为它会工作/你可以改变它
在您的Category.php模型中

public function products() {
    return $this->hasMany(Product::class);
}

单位:seeder

factory(App\Category::class, 10)->create()->each(function($c) {
    $c->products()->save(factory(App\Product::class)->make());
}); // each Category will have 1 product

Laravel数据库测试关系

相关问题