|
15 | 15 | use Filament\Schemas\Components\Contracts\HasAffixActions; |
16 | 16 | use Filament\Schemas\Schema; |
17 | 17 | use Filament\Support\Facades\FilamentIcon; |
| 18 | +use Illuminate\Database\Eloquent\Builder; |
18 | 19 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
19 | 20 | use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
20 | 21 | use Illuminate\Support\Arr; |
@@ -54,11 +55,13 @@ class SelectTree extends Field implements HasAffixActions |
54 | 55 |
|
55 | 56 | protected bool $grouped = true; |
56 | 57 |
|
57 | | - protected string|Closure $relationship; |
| 58 | + protected Closure|Builder|null $query = null; |
58 | 59 |
|
59 | | - protected ?Closure $modifyQueryUsing; |
| 60 | + protected string|Closure|null $relationship = null; |
60 | 61 |
|
61 | | - protected ?Closure $modifyChildQueryUsing; |
| 62 | + protected ?Closure $modifyQueryUsing = null; |
| 63 | + |
| 64 | + protected ?Closure $modifyChildQueryUsing = null; |
62 | 65 |
|
63 | 66 | protected Closure|int $defaultOpenLevel = 0; |
64 | 67 |
|
@@ -158,8 +161,8 @@ protected function setUp(): void |
158 | 161 | protected function buildTree(): Collection |
159 | 162 | { |
160 | 163 | // 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()); |
163 | 166 |
|
164 | 167 | // If we're not at the root level and a modification callback is provided, apply it to null query |
165 | 168 | if ($this->modifyQueryUsing) { |
@@ -272,6 +275,17 @@ public function relationship(string $relationship, string $titleAttribute, strin |
272 | 275 | return $this; |
273 | 276 | } |
274 | 277 |
|
| 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 | + |
275 | 289 | public function withCount(bool $withCount = true): static |
276 | 290 | { |
277 | 291 | $this->withCount = $withCount; |
@@ -337,11 +351,23 @@ public function append(Closure|array|null $append = null): static |
337 | 351 | return $this; |
338 | 352 | } |
339 | 353 |
|
340 | | - public function getRelationship(): BelongsToMany|BelongsTo |
| 354 | + public function getRelationship(): BelongsToMany|BelongsTo|null |
341 | 355 | { |
| 356 | + if (is_null($this->relationship)) { |
| 357 | + return null; |
| 358 | + } |
342 | 359 | return $this->getModelInstance()->{$this->evaluate($this->relationship)}(); |
343 | 360 | } |
344 | 361 |
|
| 362 | + public function getQuery(): ?Builder |
| 363 | + { |
| 364 | + if (! is_null($this->query)) { |
| 365 | + return $this->evaluate($this->query); |
| 366 | + } |
| 367 | + |
| 368 | + return $this->getRelationship()->getRelated()->query(); |
| 369 | + } |
| 370 | + |
345 | 371 | public function getTitleAttribute(): string |
346 | 372 | { |
347 | 373 | return $this->evaluate($this->titleAttribute); |
|
0 commit comments