I have 3 models: Member
, Invoice
and Payment
Here is the Member
model:
class Member extends Model
{
public function invoice()
{
return $this->hasOne(Invoice::class);
}
}
And in the Invoice
model:
class Invoice extends Model
{
public function member()
{
return $this->belongsTo(Member::class, 'member_id');
}
public function payments()
{
return $this->hasMany(Payment::class, 'invoice_id');
}
}
And lastly, the Payment
model:
class Payment extends Model
{
public function invoice()
{
return $this->belongsTo(Invoice::class, 'invoice_id');
}
}
Now, in my seeder, I want to create a Payment for each Invoice of each Member:
public function run()
{
factory(Member::class, 100)->create()->each(function ($m) {
$m->invoice()->save(factory(App\Invoice::class)->create()->each(function ($i) {
$i->payments()->save(factory(App\Payment::class)->make());
}));
});
}
But it returns an error when I try to seed:
Symfony\Component\Debug\Exception\FatalThrowableError : Type error: Argument 1 passed to Illuminate\Database\Eloquent\Relations\HasOneOrMany::save() must be an instance of Illuminate\Database\Eloquent\Model, boolean given
How can I achieve my desired output?