Laravel模型工厂一对多关系

时间:2017-04-19 08:47:22

标签: php laravel factory

我有一个模型两个表。建筑物和building_metas。一栋建筑有许多类似的东西:

buildings
---------------
id
user_id
lot_number
price
status_tx_id
lot_number_tx_id

building_metas
-----------
id
own_id
taxonomy_id
value

我有一个默认工厂

$factory->define(App\Building::class, function (Faker\Generator $faker) {

    $types = [
        config('taxonomies.own_types.apartment.id'),
        config('taxonomies.own_types.ground.id'),
        config('taxonomies.own_types.house.id')
    ];

    $statuses = [
        config('taxonomies.own_statuses.ACCEPTED'),
        config('taxonomies.own_statuses.ACTIVE'),
        config('taxonomies.own_statuses.DELETED'),
        config('taxonomies.own_statuses.PAYED'),
        config('taxonomies.own_statuses.RESERVED'),
        config('taxonomies.own_statuses.TMP'),
    ];

    $lotNumberStatuses = [
        config('taxonomies.lot_number_statuses.accepted.id'),
        config('taxonomies.lot_number_statuses.default.id'),
        config('taxonomies.lot_number_statuses.rejected.id'),
    ];

    return [
        'user_id' => function() {
            return factory(App\User::class)->create()->id;
        },
        'lot_number' => $faker->regexify('[0-9]{3,6}(\/[1-9]{1}(\/[A-Z]{1}\/[1-9]{1,2})?)?'),
        'price' => $faker->randomNumber(),
        'status_tx_id' => $faker->randomElement($statuses),
        'lot_number_status_tx_id' => $faker->randomElement($lotNumberStatuses)
    ];
});

我如何在这家工厂创建元素?

我想使用laravel模型工厂,所以当我想创建一个新建筑时,我想要它创建metas。这样的事情:

factory(App\Building::class)->create();

它创建了一个带有metas的建筑。有可能吗?

0 个答案:

没有答案