From 7d882686a4791391a4f50064f1d7555169f2abeb Mon Sep 17 00:00:00 2001 From: Wendell Adriel Date: Mon, 28 Nov 2022 15:08:52 +0000 Subject: [PATCH] [9.x] Prevent test issues with relations with the $touches property (#45118) * #45064 - Added function to remove empty relationships that were causing issues with touched relationships on tests * #45064 - Fixed code style issue * formatting Co-authored-by: Taylor Otwell --- Eloquent/Factories/Factory.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Eloquent/Factories/Factory.php b/Eloquent/Factories/Factory.php index 389e47baa..a3444ccbb 100644 --- a/Eloquent/Factories/Factory.php +++ b/Eloquent/Factories/Factory.php @@ -328,6 +328,12 @@ protected function store(Collection $results) $model->save(); + foreach ($model->getRelations() as $name => $items) { + if ($items->isEmpty()) { + $model->unsetRelation($name); + } + } + $this->createChildren($model); }); }