Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Feb 3, 2021
1 parent 354c634 commit a4cdd55
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/vendor
.env
.phpunit.result.cache
.php_cs.cache
.php_cs.cache
composer.lock
22 changes: 12 additions & 10 deletions src/JSON.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Naoray\NovaJson;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Resources\MergeValue;
use Laravel\Nova\Makeable;
use Illuminate\Support\Str;
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Fields\Hidden;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Resources\MergeValue;
use Laravel\Nova\Http\Requests\NovaRequest;
use Laravel\Nova\Makeable;
use Naoray\NovaJson\Exceptions\AttributeCast;

class JSON extends MergeValue
Expand Down Expand Up @@ -66,17 +66,19 @@ public function fillAtOnce(callable $fillOnceCallback = null)

$this->data[] = Hidden::make($this->attribute)->fillUsing(function (NovaRequest $request, $model, $attribute, $requestAttribute) use ($fillOnceCallback) {
$keys = collect($request->keys())
->filter(fn ($key) => Str::startsWith($key, $this->attribute))
->filter(fn ($key) => Str::startsWith($key, $this->attribute) && $key !== $this->attribute)
->all();

$requestValues = collect($request->only($keys))
->mapWithKeys(function ($value, $key) use ($request, $model) {
$value = $this->fetchValueFromRequest($request, $model, $key, $key);
$key = Str::after($key, $this->attribute . '->');

return [$key => $value];
return [$key => $this->fetchValueFromRequest($request, $model, $key, $key)];
})
->all();
->reduceWithKeys(function ($carry, $item, $key) {
$path = str_replace('->', '.', $key);
data_set($carry, $path, $item);

return $carry;
}, [])[$this->attribute];

$model->{$attribute} = $fillOnceCallback
? $fillOnceCallback($request, $requestValues, $model, $attribute, $requestAttribute)
Expand Down Expand Up @@ -151,7 +153,7 @@ protected function prepareField(Field $field)
return;
}

if (! $model->hasCast($this->attribute)) {
if (!$model->hasCast($this->attribute)) {
throw AttributeCast::notFoundFor($this->attribute);
}

Expand Down

0 comments on commit a4cdd55

Please sign in to comment.