Skip to content

Commit 090ac24

Browse files
committed
Support usage without relationship
1 parent 4428d1f commit 090ac24

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ SelectTree::make('category_id')
3838
->relationship('category', 'name', 'parent_id')
3939
```
4040

41+
## Usage without relationships
42+
43+
Use the tree without relationship
44+
45+
```php
46+
SelectTree::make('category_id')
47+
->query(fn() => Category::query(), 'name', 'parent_id')
48+
```
49+
4150
## Custom Query
4251

4352
Customize the parent query

src/SelectTree.php

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Filament\Schemas\Components\Contracts\HasAffixActions;
1616
use Filament\Schemas\Schema;
1717
use Filament\Support\Facades\FilamentIcon;
18+
use Illuminate\Database\Eloquent\Builder;
1819
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1920
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
2021
use Illuminate\Support\Arr;
@@ -54,11 +55,13 @@ class SelectTree extends Field implements HasAffixActions
5455

5556
protected bool $grouped = true;
5657

57-
protected string|Closure $relationship;
58+
protected Closure|Builder|null $query = null;
5859

59-
protected ?Closure $modifyQueryUsing;
60+
protected string|Closure|null $relationship = null;
6061

61-
protected ?Closure $modifyChildQueryUsing;
62+
protected ?Closure $modifyQueryUsing = null;
63+
64+
protected ?Closure $modifyChildQueryUsing = null;
6265

6366
protected Closure|int $defaultOpenLevel = 0;
6467

@@ -158,8 +161,8 @@ protected function setUp(): void
158161
protected function buildTree(): Collection
159162
{
160163
// Start with two separate query builders
161-
$nullParentQuery = $this->getRelationship()->getRelated()->query()->where($this->getParentAttribute(), $this->getParentNullValue());
162-
$nonNullParentQuery = $this->getRelationship()->getRelated()->query()->whereNot($this->getParentAttribute(), $this->getParentNullValue());
164+
$nullParentQuery = $this->getQuery()->clone()->where($this->getParentAttribute(), $this->getParentNullValue());
165+
$nonNullParentQuery = $this->getQuery()->clone()->whereNot($this->getParentAttribute(), $this->getParentNullValue());
163166

164167
// If we're not at the root level and a modification callback is provided, apply it to null query
165168
if ($this->modifyQueryUsing) {
@@ -272,6 +275,17 @@ public function relationship(string $relationship, string $titleAttribute, strin
272275
return $this;
273276
}
274277

278+
public function query(Builder|Closure|null $query, string $titleAttribute, string $parentAttribute, ?Closure $modifyQueryUsing = null, ?Closure $modifyChildQueryUsing = null): static
279+
{
280+
$this->query = $query;
281+
$this->titleAttribute = $titleAttribute;
282+
$this->parentAttribute = $parentAttribute;
283+
$this->modifyQueryUsing = $modifyQueryUsing;
284+
$this->modifyChildQueryUsing = $modifyChildQueryUsing;
285+
286+
return $this;
287+
}
288+
275289
public function withCount(bool $withCount = true): static
276290
{
277291
$this->withCount = $withCount;
@@ -333,11 +347,23 @@ public function append(Closure|array|null $append = null): static
333347
return $this;
334348
}
335349

336-
public function getRelationship(): BelongsToMany|BelongsTo
350+
public function getRelationship(): BelongsToMany|BelongsTo|null
337351
{
352+
if (is_null($this->relationship)) {
353+
return null;
354+
}
338355
return $this->getModelInstance()->{$this->evaluate($this->relationship)}();
339356
}
340357

358+
public function getQuery(): ?Builder
359+
{
360+
if (! is_null($this->query)) {
361+
return $this->evaluate($this->query);
362+
}
363+
364+
return $this->getRelationship()->getRelated()->query();
365+
}
366+
341367
public function getTitleAttribute(): string
342368
{
343369
return $this->evaluate($this->titleAttribute);

0 commit comments

Comments
 (0)