Laravel factory model relationship of one to great

I have two models. Buildings and building_metas. One building has many such metases:

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

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

      

I have a default factory

$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)
    ];
});

      

How can I create meta in this factory?

I want to use the laravel model factory, so when I want to create a new building, I want it to create meta files. Something like that:

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

      

And he creates a building with metases. Is it possible?

+3


source to share





All Articles