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()"?
CodePudding user response:
I would suggest that you look into the validation functionallities of laravel. (https://laravel.com/docs/8.x/validation)
you can make nested validations, so for example if you want to validate the seats of a car you can make rules like this:
public function store(Request $request)
{
$validated = $this->validate($request, [
'name' => 'required|string',
'model' => 'required|exists:car_models,name',
'seats' => 'required|array',
'seats.*.color' => 'required',
'seats.*.width' => 'numeric',
'seats.*.fabric' => 'required|string',
]);
// create the car with all relation data
return $car;
}
The validation could be done as shown above, or via form request validation (https://laravel.com/docs/8.x/validation#form-request-validation).
That way, you can be sure that the users input is valid and will work before any of the models are created. After that you should create the car and add all the relations after. I would however suggest that you use the eloquent relations instead, by doing that you can write something like
// Create relation model with array of data
$car->seats()->create($seatData);
// Create relation models with collection of data
$car->seats()->createMany($seatsDataCollection);