diff --git a/src/Helpers/Fluent.php b/src/Helpers/Fluent.php new file mode 100644 index 0000000..79d03d9 --- /dev/null +++ b/src/Helpers/Fluent.php @@ -0,0 +1,47 @@ + $value) { + $attribute = Str::replace('->', '.', $key); + + if (! Arr::has($this->attributes, $attribute)) { + Arr::set($this->attributes, $attribute, $value); + } + } + + return $this; + } + + /** + * Fill the model with an array of attributes. + * + * @param array $attributes + * + * @return $this + */ + public function forceFill(array $attributes) + { + foreach ($attributes as $key => $value) { + $attribute = Str::replace('->', '.', $key); + + Arr::set($this->attributes, $attribute, $value); + } + + return $this; + } +} diff --git a/src/NovaInlineRelationship.php b/src/NovaInlineRelationship.php index 77256d0..627508d 100644 --- a/src/NovaInlineRelationship.php +++ b/src/NovaInlineRelationship.php @@ -9,7 +9,6 @@ use Laravel\Nova\Fields\ID; use Laravel\Nova\Fields\File; use Laravel\Nova\Fields\Field; -use Laravel\Nova\Support\Fluent; use Illuminate\Http\UploadedFile; use Illuminate\Support\Collection; use Laravel\Nova\ResourceToolElement; @@ -18,6 +17,7 @@ use Laravel\Nova\Http\Requests\NovaRequest; use Laravel\Nova\Fields\ResolvesReverseRelation; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use KirschbaumDevelopment\NovaInlineRelationship\Helpers\Fluent; use KirschbaumDevelopment\NovaInlineRelationship\Rules\RelationshipRule; use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; use KirschbaumDevelopment\NovaInlineRelationship\Traits\RequireRelationship; @@ -254,17 +254,16 @@ public function getValueFromField(Field $field, NovaInlineRelationshipRequest $r // Fill Attributes in Field $field->fillAttribute($request, $attribute, $temp, $attribute); - return $temp->{$attribute} ?? null; + return $temp->{$attribute} ?? data_get($temp, "forceFill.{$attribute}"); } /** - * Serialize options for the field. - * - * @param bool $searchable - * @param mixed $optionsCallback - * - * @return array> - */ + * Serialize options for the field. + * + * @param mixed $optionsCallback + * + * @return array> + */ public function serializeOptions($optionsCallback) { $options = value($optionsCallback); @@ -537,18 +536,17 @@ protected function updateFieldValue($resource, $attribute, Collection $propertie protected function getResourceResponse(NovaRequest $request, $response, Collection $properties): array { return collect($response)->map(function ($itemData, $weight) use ($properties, $request) { - $item = $itemData['values']; - $modelId = $itemData['modelId']; + $item = data_get($itemData, 'values'); + $modelId = data_get($itemData, 'modelId'); $fields = collect($item)->map(function ($value, $key) use ($properties, $request, $item) { if ($properties->has($key)) { $field = $this->getResourceField($properties->get($key), $key); $newRequest = $this->getDuplicateRequest($request, $item); - return $this->getValueFromField($field, $newRequest, $key) - ?? ($field instanceof File) && ! empty($value) + return ($field instanceof File) && ! empty($value) ? $value - : null; + : $this->getValueFromField($field, $newRequest, $key); } return $value;