From a6cf8e465f85507430798f62d7e9f820a832be6e Mon Sep 17 00:00:00 2001 From: Max Korlaar Date: Sat, 20 Mar 2021 15:50:12 +0100 Subject: [PATCH 1/2] Allow options to be passed as a callable or closure as well, allowing options to be loaded only when the component is shown, instead of every time the component is created. --- src/BelongsToManyField.php | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/BelongsToManyField.php b/src/BelongsToManyField.php index 0f0753c..101898f 100644 --- a/src/BelongsToManyField.php +++ b/src/BelongsToManyField.php @@ -10,6 +10,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; @@ -35,9 +42,10 @@ class BelongsToManyField extends Field /** * Create a new field. * - * @param string $name - * @param string|null $attribute - * @param string|null $resource + * @param string $name + * @param string|null $attribute + * @param string|null $resource + * * @return void */ //Code by @drsdre @@ -77,10 +85,11 @@ public function optionsLabel(string $optionsLabel) return $this->withMeta(['optionsLabel' => $this->label]); } - public function options($options) + public function options($options = []) { - $options = collect($options); - return $this->withMeta(['options' => $options]); + $this->optionsCallback = $options; + + return $this; } public function relationModel($model) @@ -146,6 +155,14 @@ public function resolve($resource, $attribute = null) public function jsonSerialize() { + if (isset($this->optionsCallback)) { + if (is_callable($this->optionsCallback)) { + $this->withMeta(['options' => call_user_func($this->optionsCallback)]); + } else { + $this->withMeta(['options' => collect($this->optionsCallback)]); + } + } + return array_merge([ 'attribute' => $this->attribute, 'component' => $this->component(), From 48a8f906cf52a4c5807d85245ced22d9c03e958f Mon Sep 17 00:00:00 2001 From: Max Korlaar Date: Sat, 20 Mar 2021 21:35:44 +0100 Subject: [PATCH 2/2] Extract logic to its own function to improve readability --- src/BelongsToManyField.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/BelongsToManyField.php b/src/BelongsToManyField.php index 101898f..f945a8e 100644 --- a/src/BelongsToManyField.php +++ b/src/BelongsToManyField.php @@ -155,13 +155,7 @@ public function resolve($resource, $attribute = null) public function jsonSerialize() { - if (isset($this->optionsCallback)) { - if (is_callable($this->optionsCallback)) { - $this->withMeta(['options' => call_user_func($this->optionsCallback)]); - } else { - $this->withMeta(['options' => collect($this->optionsCallback)]); - } - } + $this->resolveOptions(); return array_merge([ 'attribute' => $this->attribute, @@ -195,4 +189,15 @@ public function setPivot(array $attributes) return $this; } + + 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)]); + } + } + } }