From 080e101b24b038737f810eeda2f000ff8b4c9663 Mon Sep 17 00:00:00 2001 From: Rudie Dirkx Date: Mon, 21 Mar 2022 16:35:12 +0100 Subject: [PATCH 1/2] always try to return a real makeNewEmptyModel --- .../Fields/CollectionType.php | 27 ++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Kris/LaravelFormBuilder/Fields/CollectionType.php b/src/Kris/LaravelFormBuilder/Fields/CollectionType.php index 760c3be4..0c2d3426 100644 --- a/src/Kris/LaravelFormBuilder/Fields/CollectionType.php +++ b/src/Kris/LaravelFormBuilder/Fields/CollectionType.php @@ -155,7 +155,32 @@ protected function makeEmptyRowValue() protected function makeNewEmptyModel() { - return value($this->getOption('empty_model')); + if ($empty = $this->getOption('empty_model')) { + return value($empty); + } + + $parent = $this->getParent()->getModel(); + if ($parent instanceof \Illuminate\Database\Eloquent\Model) { + if (method_exists($parent, $this->name)) { + $relation = call_user_func([$parent, $this->name]); + if ($relation instanceof \Illuminate\Database\Eloquent\Relations\Relation) { + $model = $relation->getRelated()->newInstance()->setAttribute( + $relation->getForeignKeyName(), + $parent->{$relation->getLocalKeyName()} + ); + return $model; + } + } + } + + if (($data = $this->getOption('data')) && count($data)) { + $model = reset($data); + if ($model instanceof Model) { + return new $model; + } + } + + return null; } protected function formatInputIntoModels(array $input, array $originalData = []) From 9ec2ab88f3e45e96297addeb984a29aaa12eba68 Mon Sep 17 00:00:00 2001 From: Rudie Dirkx Date: Wed, 19 Apr 2023 21:32:52 +0200 Subject: [PATCH 2/2] collection vs array --- .../LaravelFormBuilder/Fields/CollectionType.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/Kris/LaravelFormBuilder/Fields/CollectionType.php b/src/Kris/LaravelFormBuilder/Fields/CollectionType.php index ccd0cb2f..7ef74f70 100644 --- a/src/Kris/LaravelFormBuilder/Fields/CollectionType.php +++ b/src/Kris/LaravelFormBuilder/Fields/CollectionType.php @@ -3,6 +3,7 @@ namespace Kris\LaravelFormBuilder\Fields; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Collection; class CollectionType extends ParentType @@ -183,10 +184,10 @@ protected function makeNewEmptyModel() } $parent = $this->getParent()->getModel(); - if ($parent instanceof \Illuminate\Database\Eloquent\Model) { + if ($parent instanceof Model) { if (method_exists($parent, $this->name)) { $relation = call_user_func([$parent, $this->name]); - if ($relation instanceof \Illuminate\Database\Eloquent\Relations\Relation) { + if ($relation instanceof Relation) { $model = $relation->getRelated()->newInstance()->setAttribute( $relation->getForeignKeyName(), $parent->{$relation->getLocalKeyName()} @@ -197,9 +198,11 @@ protected function makeNewEmptyModel() } if (($data = $this->getOption('data')) && count($data)) { - $model = reset($data); - if ($model instanceof Model) { - return new $model; + foreach ($data as $model) { + if ($model instanceof Model) { + return new $model; + } + break; } }