Antonella's avatar

filtar table dropdown livewire

I have this livewire component which filters a table based on the two dropdown buttons. one is called Instance and the other Job. I would like to find a way to filter both buttons.

in this way or I pass it to render updatejob or updateinstance. So only one of the two buttons works. is there any way to make them both work?

in the sense that if I use a public variable to which I pass the query results through the two methods it does not allow me to do the pagination since I would operate its public variable that allows collections

namespace App\Http\Livewire;
    use Livewire\WithPagination;
    
    use App\Models\Task;
    use Livewire\Component;
    
    class TableTodo extends Component
    {
        use WithPagination;
    
        public $instance;
        public $job;
        public $work;
        // public $tasks;
        public $elements;
    
        public function updatinginstance()
        {
            $this->resetPage();
        }
    
        public function mount()
        {
            $this->instance='';
            $this->job='';
            // $this->tasks=[];
            $this->elements=[];
            $this->work=[];
        }
    
        public function updatedelements()
        {
            $in = Task::find($this->instance);
    
            if(!empty($in))
            {
                $tasks= Task::whereIn('process_status', ['B','C'];
            }
            else
            {
                $tasks= Task::whereIn('process_status', ['A'])
                ->orderByRaw('FIELD(process_status, "new", "processing")asc')
                ->orderBy('created_at', 'asc')
                ->paginate(50)
                ;
            }
    
            return $tasks;
    
        }
    
        public function updatedwork()
        {
    
            $in_job = Task::find($this->job);
    
            if(!empty($in_job))
            {
                $tasks = Task::whereIn('process_status', ['A'])
                ->where('job', 'like', $in_job->job)
                ->orderBy('created_at', 'asc')
                ->paginate(50);
            }
            else
            {
                $tasks = Task::whereIn('process_status', ['A'])
                ->orderBy('created_at', 'asc')
                ->paginate(50);
            }
    
            return $tasks;
        }
    
    
        public function render()
        {
            $this->elements = Task::whereIn('process_status', ['A'])->orderBy('created_at', 'asc')->get();
    
            $this->work = Task::whereIn('process_status', ['A'])->orderBy('created_at', 'asc')->get();
            $tasks = $this->updatedjob();
            $tasks = $this->updatedinstance();
    
            return view('livewire.table-todo',['tasks' => $tasks]);
        }
    }
0 likes
1 reply
Snapey's avatar

Your code does not make ANY sense.

For instance, $tasks contains one thing, then is immediately overwritten

            $tasks = $this->updatedjob();
            $tasks = $this->updatedinstance();

Render method. Perform your query of the Task model

If $this->job is set then apply the filter to the query

if $this-instance is set then apply filter to the query

so you will have tasks with none, one or two filters.

I can't be more specific because I don't know your data or your view

There is no need to query data anywhere else other than the render method.

Please or to participate in this conversation.