Laravel 9工厂并创建相关的1:1型号

发布于 2025-02-08 06:56:49 字数 1136 浏览 3 评论 0原文

我想使用Laravel工厂在我的测试环境中填充一些虚拟数据。我有一个位置模型和一个位置尾声模型,它们具有1:1的关系,只要创建一个新的位置,就需要在locationDetails表中创建记录。

我该如何在工厂中进行此操作,以使每个位置记录的 1个位置通讯记录。

这是我在Databaseseeder类中的内容:

    if(env('APP_ENV') === 'local') {
        Client::factory()->count(25)->create();
        Location::factory()->count(30)->create();
    }

我的位置工厂定义:

public function definition()
    {
        return [
            'name' => $this->faker->company,
            'client_id' => Client::all()->random()->id,
            'location_status_id' => LocationStatus::all()->random()->id,
            'address' => $this->faker->streetAddress,
            'city' => $this->faker->city,
            'state' => $this->faker->StateAbbr,
            'zip_code' => $this->faker->numerify('#####'),
            'phone' => $this->faker->numerify('###-###-####'),
            'email' => $this->faker->safeEmail,
            'is_onboarding_completed' => 1,
        ];
    }

Laravel看起来有一个工厂的AfterCreate()回调,但我不清楚如何完成此操作。

I'd like to use Laravel factories to populate some dummy data in my test environment. I have a Locations model and a LocationDetails model, which have a 1:1 relationship and a record needs to be created in the LocationDetails table whenever a new Location is created.

How can I do this in a factory such that there's exactly 1 LocationDetails record for each Location record.

Here's what I have in my DatabaseSeeder class:

    if(env('APP_ENV') === 'local') {
        Client::factory()->count(25)->create();
        Location::factory()->count(30)->create();
    }

My location factory definition:

public function definition()
    {
        return [
            'name' => $this->faker->company,
            'client_id' => Client::all()->random()->id,
            'location_status_id' => LocationStatus::all()->random()->id,
            'address' => $this->faker->streetAddress,
            'city' => $this->faker->city,
            'state' => $this->faker->StateAbbr,
            'zip_code' => $this->faker->numerify('#####'),
            'phone' => $this->faker->numerify('###-###-####'),
            'email' => $this->faker->safeEmail,
            'is_onboarding_completed' => 1,
        ];
    }

It looks like Laravel has an afterCreate() callback for a factory, but I'm just not clear on how to accomplish this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她比我温柔 2025-02-15 06:56:49

这项工作

Location::factory()
    ->has(LocationDetails::factory())
    ->count(30)
    ->create();

是否可以在 Location -factory 上定义一个状态

//LocationFactory
//Assuming location_id is the foreign key on LocationDetails model

public function withLocationDetail()
{
    return $this->afterCreating(function (Location $location) {
        LocationDetails::factory()->create(['location_id' => $location->id]);
    });
}

,然后像

Location::factory()
    ->withLocationDetail()
    ->count(30)
    ->create();

Does this work

Location::factory()
    ->has(LocationDetails::factory())
    ->count(30)
    ->create();

Or you can have a state defined on the LocationFactory

//LocationFactory
//Assuming location_id is the foreign key on LocationDetails model

public function withLocationDetail()
{
    return $this->afterCreating(function (Location $location) {
        LocationDetails::factory()->create(['location_id' => $location->id]);
    });
}

And then use it like

Location::factory()
    ->withLocationDetail()
    ->count(30)
    ->create();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文