Skip to content
This repository has been archived by the owner on Jul 5, 2024. It is now read-only.

Commit

Permalink
Fixed fillAttributeFromRequest and compiled js fields (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
kayacekovic authored Jan 21, 2021
1 parent 6068993 commit 9584127
Show file tree
Hide file tree
Showing 2 changed files with 28,302 additions and 32 deletions.
28,267 changes: 28,266 additions & 1 deletion dist/js/field.js

Large diffs are not rendered by default.

67 changes: 36 additions & 31 deletions src/DynamicSelect.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace Hubertnnn\LaravelNova\Fields\DynamicSelect;

use RuntimeException;
use Hubertnnn\LaravelNova\Fields\DynamicSelect\Traits\DependsOnAnotherField;
use Hubertnnn\LaravelNova\Fields\DynamicSelect\Traits\HasDynamicOptions;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\Field;
use Laravel\Nova\Http\Requests\NovaRequest;

class DynamicSelect extends Field
{
Expand Down Expand Up @@ -34,49 +36,50 @@ public function belongsToMany($resourceClass)
{
$model = $resourceClass::$model;
$primaryKey = (new $model)->getKeyName();
$this->multiselect = true;

$this->resolveUsing(function ($value) use ($primaryKey, $resourceClass) {
$value = collect($value)->map(function ($option) use ($primaryKey) {
return [
'label' => $option->{$this->labelKey ?: 'name'},
'value' => $option->{$primaryKey},
];
});
if ($this->multiselect) {
$this->resolveUsing(function ($value) use ($primaryKey, $resourceClass) {
$value = collect($value)->map(function ($option) use ($primaryKey) {
return [
'label' => $option->{$this->labelKey ?: 'name'},
'value' => $option->{$primaryKey},
];
});

return $value;
});
return $value;
});

$this->fillUsing(function ($request, $model, $requestAttribute, $attribute) {
$model::saved(function ($model) use ($attribute, $request) {
// Validate
if (!method_exists($model, $attribute)) {
throw new RuntimeException("{$model}::{$attribute} must be a relation method.");
}
$this->fillUsing(function ($request, $model, $requestAttribute, $attribute) {
$model::saved(function ($model) use ($requestAttribute, $request) {
// Validate
if (!method_exists($model, $requestAttribute)) {
throw new RuntimeException("{$model}::{$requestAttribute} must be a relation method.");
}

$relation = $model->{$attribute}();
$relation = $model->{$requestAttribute}();

if (!method_exists($relation, 'sync')) {
throw new RuntimeException("{$model}::{$attribute} does not appear to model a BelongsToMany or MorphsToMany.");
}
if (!method_exists($relation, 'sync')) {
throw new RuntimeException("{$model}::{$requestAttribute} does not appear to model a BelongsToMany or MorphsToMany.");
}

$values = collect($request->get($attribute))
->filter(function ($v) {
return $v;
})
->map(function ($v) {
return json_decode($v)->value;
})->toArray();
$values = collect($request->get($requestAttribute))
->filter(function ($v) {
return $v;
})->map(function ($v) {
return json_decode($v)->value;
})->toArray();

// Sync
$relation->sync($values ?? []);
// Sync
$relation->sync($values ?? []);
});
});
});

$this->multiselect = true;
}

return $this;
}


protected function fillAttributeFromRequest(Request $request, $requestAttribute, $model, $attribute)
{
if ($this->multiselect) {
Expand All @@ -89,6 +92,8 @@ protected function fillAttributeFromRequest(Request $request, $requestAttribute,

$model->{$attribute} = $values;
}
} else {
$model->{$attribute} = $value;
}
}

Expand Down

0 comments on commit 9584127

Please sign in to comment.