3

I have a model two tables. Buildings and building_metas. One building have many metas like that:

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 metas in this factory?

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

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

And it's create a building with metas. Is it possible?

  • Possible duplicate of [Adding Relations to Laravel Factory Model](http://stackoverflow.com/questions/32540845/adding-relations-to-laravel-factory-model) – Douwe de Haan Apr 19 '17 at 08:49
  • No. I want to create a full building with metas with one line of factory(App\Building::class), becouse every time i create a new object, i have to create that metas to. – László Tóth Apr 19 '17 at 08:56
  • Check this link. I think, it might be helpfull. https://laracasts.com/discuss/channels/laravel/reference-in-seeders?page=1. Regards. – Viktor Apr 19 '17 at 09:04
  • So it's not possible. :( – László Tóth Apr 19 '17 at 09:09

0 Answers0