Skip to content

Commit

Permalink
Merge branch 'scholieren-com-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjacho committed Apr 24, 2021
2 parents a6f7956 + 73be2d7 commit d50fb24
Showing 1 changed file with 23 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/BelongsToManyField.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@

class BelongsToManyField extends Field
{
/**
* The callback to be used for the field's options.
*
* @var array|callable
*/
private $optionsCallback;

public $showOnIndex = true;
public $showOnDetail = true;
public $isAction = false;
Expand Down Expand Up @@ -83,11 +90,11 @@ public function optionsLabel(string $optionsLabel)
return $this->withMeta(['optionsLabel' => $this->label]);
}

public function options($options)
public function options($options = [])
{
$options = collect($options);
$this->optionsCallback = $options;

return $this->withMeta(['options' => $options]);
return $this;
}

public function relationModel($model)
Expand Down Expand Up @@ -168,6 +175,8 @@ public function resolve($resource, $attribute = null)

public function jsonSerialize()
{
$this->resolveOptions();

return array_merge([
'attribute' => $this->attribute,
'component' => $this->component(),
Expand Down Expand Up @@ -228,4 +237,15 @@ protected function getNoResultSlot()
{
return __('belongs-to-many-field-nova::vue-multiselect.no_result');
}

private function resolveOptions(): void
{
if (isset($this->optionsCallback)) {
if (is_callable($this->optionsCallback)) {
$this->withMeta(['options' => call_user_func($this->optionsCallback)]);
} else {
$this->withMeta(['options' => collect($this->optionsCallback)]);
}
}
}
}

0 comments on commit d50fb24

Please sign in to comment.