I'll give you a small example to explain my problem.
Tables:
main
itens
The two have a relationship, a main
has several itens
.
What I'm doing to save:
...
$main = $this->main->create($postCreate);
foreach ($postCreate['itens'] as $item){
$item['main_id'] = $main->id;
$this->itens->create($item);
}
...
It works, but if there is an error when saving the items, it is to save the main
without the items or missing some items, I wanted to save everything, and if there were any errors do not save anything.
How to save tables with relationship in Laravel?