如何将参数传递给 Laravel 工厂?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32378215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:07:08  来源:igfitidea点击:

How to pass arguments to Laravel factories?

laravelfactorylaravel-seeding

提问by Cristian

I have a userstable and a one-to-zero/one relation with a businessestable (users.user_id => businesses.user_id). On my userstable I have a discriminator which tells me if the user is of type business and therefore I need to have details on the businessestable as well.

我有一个users表和一个businesses表(users.user_id =>business.user_id)的一对零/一关系。在我的users桌子上,我有一个鉴别器,它告诉我用户是否属于业务类型,因此我也需要在businesses桌子上有详细信息。

I want to create my Users with my factory which currently is working and then only create business details where the discriminator points to a business account.

我想用我目前正在工作的工厂创建我的用户,然后只在鉴别器指向企业帐户的地方创建业务详细信息。

I have three options in my mind:

我脑子里有三个选择:

  1. Create from users factory and then using '->each()' do some checks on the discriminator and create a new business user using a the factory. However I cannot pass to the business factory the user_idthat the user was assigned.
  2. First create the users. Then in my Business seeder, retrieve all Users that match a 'business' discriminator. Then for all of these users run a factory that creates the business details. But again, I would have to link somehow the user_idof the already create user with the business factory user_id.
  3. In my business factory, create a new User and retrieve the id, thus making the link between users.user_idand business.user_id. However I am using a random generator for user.user_typeso even if I have the businessestable filled it might be for users that have the discriminator as 'personal'.
  1. 从用户工厂创建,然后使用 '->each()' 对鉴别器进行一些检查,并使用工厂创建一个新的业务用户。但是,我无法将user_id分配给用户的业务传递给工厂。
  2. 首先创建用户。然后在我的业务播种器中,检索与“业务”鉴别器匹配的所有用户。然后为所有这些用户运行一个创建业务详细信息的工厂。但同样,我必须以某种方式user_id将已经创建的用户与商业工厂联系起来user_id
  3. 在我的商业工厂中,创建一个新用户并检索 id,从而在users.user_id和之间建立链接business.user_id。但是,我使用的是随机生成器,user.user_type因此即使我businesses填写了表格,也可能适用于将鉴别器设为“个人”的用户。

Is there another way? Can I pass arguments from my Seeder to the factory?

还有其他方法吗?我可以将参数从 Seeder 传递给工厂吗?

回答by Joseph Silber

The attributes you pass to the createfunction will be passed into your model definition callback as the second argument.

您传递给create函数的属性将作为第二个参数传递到您的模型定义回调中。



In your case you don't even need to access those attributes, since they'll automatically be merged in:

在您的情况下,您甚至不需要访问这些属性,因为它们会自动合并到:

$business = factory(App\Business::class)->create();

factory(App\User::class, 5)->create([
    'business_id' => $business->id,
]);

Adapt this to your needs.

根据您的需要进行调整。

回答by Robin Hood

My code for adding polymorphic 'Admin' users was:

我添加多态“管理员”用户的代码是:

// run model factory
factory(App\Admin::class, 3)->create()->each(function ($admin) {

    $admin->user()->save(

        // solved: https://laravel.com/docs/master/database-testing#using-factories (Overriding attributes)
        factory(App\User::class)->make([
              'userable_id' => $admin->id,
              'userable_type' => App\Admin::class
        ])
    );
});

Hope this helps.

希望这可以帮助。