How do I associate other associations before saving "parent"? I have a car that have many other parts:
- A car has many seats
- A car has many floor mats
- A car has one mirror
- etc.
The thing is, if either the seats or floor mats has any defects then the car cannot be created:
$car = new Car(...);
// Many mats
$mats = [new Mat(..), new Mat(..)];
// One mirror
$mirror = new Mirror(..);
// I need to put them all together.
// This does not work
$car->saveMany([$mats, $mirror, $mats]);
// Does not work
$car->mats()->saveMany($mats);
$car->associate($mirror);
// Car should not be saved if either any of its associations have an error.
$car->save();
The docs mentioned nothing about this example when instantiating a new object then save its associations: HasMany, HasOne, BelongsTo etc
I've looked at these but cannot get my head around it:
How to "associate" "car's" associations by calling "save()"?