Reputation: 6052
I am currently building a system to manage my rental properties. I have a model that holds the properties, and each property can have many leases.
In order to bill the leases, I have a model called Invoice
which belongs to a Lease
. Further, an invoice will have many InvoiceLine
//Property.php
public function leases()
{
return $this->hasMany(Lease::class);
}
//Lease.php
public function invoices(){
return $this->hasMany(Invoice::class);
}
//Invoice.php
public function lines()
{
return $this->hasMany(InvoiceLine::class);
}
I am trying to set up seeding for this setup:
Property::factory()
->count(1)
->has(
Lease::factory()
->count(2)
->hasInvoices(1) // Where to create "Invoice Lines"?
)
->create();
Now, as you can see, when running this seeding I only create an invoice - but no invoice lines.
How can I seed an invoice with invoice lines that belongs to a Lease
?
For reference, this is how the factory classes looks for Invoice and Invoice Line:
//InvoiceFactory.php
return [
'tax' => 0,
'total' => 25000,
];
//InvoiceLineFactory.php
return [
'description' => 'Rent for April',
'tax' => 0,
'total' => 25000,
];
Upvotes: 2
Views: 185
Reputation: 139
You can simply use has
instead of hasInvoices
.
Property::factory()->has(
Lease::factory()->has(
Invoice::factory()->has(
InvoiceLine::factory()->count(3),
)->count(1)
)->count(2)
)->count(1)->create();
Upvotes: 3