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
php laravel factory


source to share


No one has answered this question yet

See similar questions:

ten
Adding Relationships to Laravel Factory Model

or similar:

464
What is the fundamental difference between factory and abstract templates?
fourteen
Laravel get class name of corresponding model
8
Defining Laravel foreign keys with model factories, one to one and one to many relationships without creating unnecessary models
6
How to use many, many polymorphic relationships in Laravel 5.2
6
Laravel 5.1 Localization Factory Seeder
4
Laravel Model Factory and Eloquent; setting dates
1
Layered factory relationships in Laravel 5.4
1
Passing array values ​​to laravel factory
0
Can I use a model in my own factory in a Laravel project?
0
Is it possible to use model factories with wrapper service in Laravel?



All Articles
Loading...
X
Show
Funny
Dev
Pics