Skip to content

Model::factory()->has() many unintended rows created #34439

Answered by hsmfawaz
hsmfawaz asked this question in General
Discussion options

You must be logged in to vote

The following syntax solve the problem

public function testCanSeedDataWithTwoLevelRelation()
    {
        $family = Family::factory();
        $products = Product::factory(1)->for($family);
        $invoice = Invoice::factory(5);
        InvoiceItem::factory(1)->has($products)->for($invoice);
        $family->count(10)->has($invoice)->create();
        $this->assertDatabaseCount('families', 10);//expected 10 got 10
        $this->assertDatabaseCount('invoices', 50);// expected 50 got 50
    }

Instead of

 public function testCanSeedDataWithTwoLevelRelation()
    {
        $invoice = Invoice::factory(5)->has(InvoiceItem::factory(1)->has(Product::factory()), 'items');
        Family::facto…

Replies: 3 comments 6 replies

Comment options

You must be logged in to vote
6 replies
@driesvints
Comment options

@hsmfawaz
Comment options

@hsmfawaz
Comment options

@driesvints
Comment options

@hsmfawaz
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by hsmfawaz
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Converted from issue

This discussion was converted from issue #34439 on September 21, 2020 16:28.