Skip to content

Commit

Permalink
Merge pull request #126 from kirschbaum-development/custom-fluent-class
Browse files Browse the repository at this point in the history
Add a custom fluent class
  • Loading branch information
harrysbaraini authored Jan 9, 2024
2 parents ae8896d + b3a60ac commit 88806f9
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 14 deletions.
47 changes: 47 additions & 0 deletions src/Helpers/Fluent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace KirschbaumDevelopment\NovaInlineRelationship\Helpers;

use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class Fluent extends \Illuminate\Support\Fluent
{
/**
* Fill the model with an array of attributes.
*
* @param array $attributes
*
* @return $this
*/
public function fill(array $attributes)
{
foreach ($attributes as $key => $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;
}
}
26 changes: 12 additions & 14 deletions src/NovaInlineRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<int, array<string, mixed>>
*/
* Serialize options for the field.
*
* @param mixed $optionsCallback
*
* @return array<int, array<string, mixed>>
*/
public function serializeOptions($optionsCallback)
{
$options = value($optionsCallback);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 88806f9

Please sign in to comment.