Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,25 @@ use CodeWithDennis\FilamentSelectTree\SelectTree;
])
```

If you need to append an item to the tree menu, use the `append` method. This method also accepts an array or a closure.

```php
->schema([
SelectTree::make('category')
->relationship('categories', 'name', 'parent_id')
->enableBranchNode()
->multiple(false)
->append([
'name' => 'Uncategorized Records',
'value' => -1,
'parent' => null, // optional
'disabled' => false, // optional
'hidden' => false, // optional
'children' => [], // optional
])
])
```

## Filters

Use the tree in your table filters. Here's an example to show you how.
Expand Down
25 changes: 23 additions & 2 deletions src/SelectTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ class SelectTree extends Field implements HasAffixActions

protected Closure|array|null $prepend = null;

protected Closure|array|null $append = null;

protected Closure|string|null $treeKey = 'treeKey';

protected function setUp(): void
Expand Down Expand Up @@ -316,6 +318,19 @@ public function prepend(Closure|array|null $prepend = null): static
return $this;
}

public function append(Closure|array|null $append = null): static
{
$this->append = $this->evaluate($append);

if (is_array($this->append) && isset($this->append['name'], $this->append['value'])) {
$this->append['value'] = (string) $this->append['value'];
} else {
throw new \InvalidArgumentException('The provided append value must be an array with "name" and "value" keys.');
}

return $this;
}

public function getRelationship(): BelongsToMany|BelongsTo
{
return $this->getModelInstance()->{$this->evaluate($this->relationship)}();
Expand Down Expand Up @@ -422,8 +437,14 @@ public function storeResults(bool $storeResults = true): static

public function getTree(): Collection|array
{
return $this->evaluate($this->buildTree()->when($this->prepend,
fn (Collection $tree) => $tree->prepend($this->evaluate($this->prepend))));
return $this->evaluate($this->buildTree()
->when($this->prepend,
fn (Collection $tree) => $tree->prepend($this->evaluate($this->prepend))
)
->when($this->append,
fn(Collection $tree) => $tree->push($this->evaluate($this->append))
)
);
}

public function getResults(): Collection|array|null
Expand Down