Search code examples
phplaravellaravel-9

Update/Insert parent with child based on validation rules Laravel


I want to update/insert my parent directly with my child instead of separated them. After my validation rule has passed.

my code:

$validatedData = $request->validate([
    'id' => 'bail|integer',
    'code' => 'bail|integer',
    'name' => 'bail|required|string|max:255',
    'child' => 'bail|array',
    'child.something' => 'bail|integer',
]);

$parent = Parent::updateOrCreate(['id' => $request->id, 'code' => $request->code], $validatedData);

Solution

  • You can't. You need to update the child after the parent is successfully updated.