FilamentF
Filament15mo ago
Asmit

Custom field dynamic query

Here I have to create a custom field where I have to show suggestion by type [ char and variable name.
I want to make it generic where user should pass the query builder and suggest according to the user enter value.

DynamicVairable::make('dynamic_varibale')
  ->modifyingQuery(function () {
      return Notification::query();
  })

But I don't know how to make it dynamic

class DynamicVairable extends Field
{
    protected string $view = 'forms.components.variable-field';

    public string $text = '';


    public Builder |\Closure $modifyingQuery;

    public function modifyingQuery(Builder | \Closure $modifyingQuery): static
    {
        $this->modifyingQuery = $modifyingQuery;

        return $this;
    }

    public function getModifyingQuery()
    {
        if($this->modifyingQuery instanceof \Closure) {
            return ($this->modifyingQuery)();
        }
        return $this->modifyingQuery;
    }

    public function searchVariable(string $value)
    {
        $this->getModifyingQuery()->where('variable', 'like', '%'.$value.'%')->get();
    }
}


This is my custom field
<x-dynamic-component
    :component="$getFieldWrapperView()"
    :field="$field"
>
    <div x-data="{ state: $wire.$entangle('{{ $getStatePath() }}') }">
      <input x-model="state">
    </div>
</x-dynamic-component>

Please suggest some idea
I tried with livewire component but from field class I'm not able to pass query builder
Was this page helpful?