Skip to content

Nested model configuration #57

@booni3

Description

@booni3

Is it currently possible to achieve something like this?

We have the relationship:

Order -> hasMany -> OrderItems

I want to create an order with a status of new and then create 4 order items with a status of allocated. Is it possible to hook into the orderItemFactory method from within the with method, and apply the allocated status?

// Test
OrderFactory::new()
    ->withOrderItems(5)
    ->create();

// OrderFactory.php
public function withOrderItems($qty)
{
    return $this->with(
        OrderItem::class,
        'orderItems',
        $qty
    );
}

// OrderItemFactory.php
public function allocated()
{
    return tap(clone $this)->overwriteDefaults([
        'state' => 'allocated',
    ]);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions