diff --git a/README.md b/README.md index 84e4337..d24fc9f 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,19 @@ For example: you have id, code and parent_code. Your model uses id as key, but t ->withKey('code') ``` +Store fetched models for additional functionality + +```PHP +->storeResults() +``` + +Now you can access the results in `disabledOptions` or `hiddenOptions` + +```PHP +->disabledOptions(function ($state, SelectTree $component) { + $results = $component->getResults(); +}) +``` ## Filters diff --git a/src/SelectTree.php b/src/SelectTree.php index f4cb349..3846aa6 100644 --- a/src/SelectTree.php +++ b/src/SelectTree.php @@ -82,6 +82,10 @@ class SelectTree extends Field implements HasAffixActions protected Closure|bool|null $withTrashed = false; + protected bool $storeResults = false; + + protected Collection|array|null $results = null; + protected function setUp(): void { // Load the state from relationships using a callback function. @@ -164,6 +168,11 @@ private function buildTree(): Collection // Combine the results from both queries $combinedResults = $nullParentResults->concat($nonNullParentResults); + // Store results for additional functionality + if ($this->storeResults) { + $this->results = $combinedResults; + } + return $this->buildTreeFromResults($combinedResults); } @@ -374,11 +383,23 @@ public function enableBranchNode(bool $enableBranchNode = true): static return $this; } + public function storeResults(bool $storeResults = true): static + { + $this->storeResults = $storeResults; + + return $this; + } + public function getTree(): Collection|array { return $this->evaluate($this->buildTree()); } + public function getResults(): Collection|array|null + { + return $this->evaluate($this->results); + } + public function getExpandSelected(): bool { return $this->evaluate($this->expandSelected);