Skip to content

Commit

Permalink
Merge pull request #80 from CodeWithDennis/feature/introduce-with-tra…
Browse files Browse the repository at this point in the history
…shed-method

Introduce `withTrashed()` method
  • Loading branch information
CodeWithDennis authored Feb 12, 2024
2 parents db8ca2e + 863980b commit 5a1b15d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,12 @@ Hide specific options in the tree
->hiddenOptions([2, 3, 4])
```

Allow soft deleted items to be displayed

```PHP
->withTrashed()
```

Specify a different key for your model.
For example: you have id, code and parent_code. Your model uses id as key, but the parent-child relation is established between code and parent_code

Expand Down
18 changes: 16 additions & 2 deletions src/SelectTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ class SelectTree extends Field implements HasAffixActions

protected ?Closure $createOptionUsing = null;

protected Closure|bool|null $withTrashed = false;

protected function setUp(): void
{
// Load the state from relationships using a callback function.
Expand Down Expand Up @@ -133,8 +135,8 @@ private function buildTree(): Collection
}

// Fetch results for both queries
$nullParentResults = $nullParentQuery->get();
$nonNullParentResults = $nonNullParentQuery->get();
$nullParentResults = $nullParentQuery->withTrashed($this->withTrashed)->get();
$nonNullParentResults = $nonNullParentQuery->withTrashed($this->withTrashed)->get();

// Combine the results from both queries
$combinedResults = $nullParentResults->concat($nonNullParentResults);
Expand Down Expand Up @@ -230,6 +232,13 @@ public function withCount(bool $withCount = true): static
return $this;
}

public function withTrashed(bool $withTrashed = true): static
{
$this->withTrashed = $withTrashed;

return $this;
}

public function direction(string $direction): static
{
$this->direction = $direction;
Expand Down Expand Up @@ -356,6 +365,11 @@ public function getGrouped(): bool
return $this->evaluate($this->grouped);
}

public function getWithTrashed(): bool
{
return $this->evaluate($this->withTrashed);
}

public function getIndependent(): bool
{
return $this->evaluate($this->independent);
Expand Down

0 comments on commit 5a1b15d

Please sign in to comment.