Antonella's avatar

Livewire:Search with Dynamic Cascading Dropdown -> Undefined variable: event

I have a problem with Dynamic Cascading Dropdown with Livewire. I'm basically creating a table that is filtered based on two parameters:

Component:

<?php

namespace App\Http\Livewire;

use Livewire\Component;
use App\Models\Task;


class Search extends Component
{
    public $poll;
    public $type_poll;
    public $type_polls;

    public $tasks;
    public $polls;

    public function mount($poll,$type_poll)
    {
        $this->poll=$poll;
        $this->type_poll=$type_poll;
        $this->tasks=[];
        $this->polls=[];
        $this->type_polls=[];

    }

    public function updatedpoll()
    {

        $this->tasks= Task::where('process_status', '=', 'new')
        ->where('id','=', $this->poll)
        ->orderBy('created_at', 'asc')
        ->get()
        ->toArray();

        $this->type_polls = Task::where('process_status', '=', 'rich')
                        ->orWhere('poll', '=', $this->poll)
                        ->orderBy('created_at', 'asc')->get();
    }

    public function render()
    {
        $this->polls = Task::where('process_status', '=', 'rich')->orderBy('created_at', 'asc')->get();
        return view('livewire.search');
    }
}

view:

 <div>
    <div class="mb-8">
        <label class="inline-block w-32 font-bold">Filter:</label>
        <select name="poll" class="border shadow p-2 bg-white" wire:model='poll'
        >
            <option value=''>Choose a poll</option>
            @foreach($polls as $poll)
                <option value={{ $poll->id }}>{{ $poll->poll }}</option>
            @endforeach
        </select >
        <select name="type_poll" class="border shadow p-2 bg-white" wire:model='type_poll'
        >
            <option value=''>Choose a type_poll</option>
            @foreach($type_polls as $type_poll)
                <option value={{ $type_poll->id }}>{{ $type_poll->type_poll }}</option>
            @endforeach
        </select >
        <table class="absolute shadow bg-white top-100 z-40 w-full lef-0 rounded max-h-select overflow-y-auto svelte-5uyqqj">
            <div class="flex flex-col w-full">
                <div class="cursor-pointer w-full border-gray-100 rounded-t border-b hover:bg-teal-100">

            @foreach ($tasks as $task)
            <tr>
                <td>
                     {{ $task['id'] }}
                </td>
                <td>
                {{ $task['poll'] }}
                </td>
                <td>
                  {{ $task['type_poll'] }}
                </td>
            </tr>

            @endforeach
                </div>
            </div>
        </table>

I call the component in the blade view:

<div class="-my-2 py-2 overflow-x-auto sm:-mx-6 sm:px-6 lg:-mx-8 lg:px-8">
                        @livewire('search',['poll'=>$event->poll, 'type_poll'=>$event->type_poll])
                    </div>

give me following error:

Undefined variable: event (View: /Users/L8_test/resources/views/test.blade.php)

0 likes
9 replies
Snapey's avatar

So your livewire code and view is irrelevant,

@livewire('search',['instance'=>$event->instance, 'job'=>$event->job])

looks like $event is not passed to your view.

Snapey's avatar

Thats my article.

You are passing event to the livewire component, but don't know what its for? You need to adapt any tutorial to your own circumstances.

What are instance and job in your mount methods?

Antonella's avatar

are the two parameters with filter the table. you can consider as country and city. @snapey

Snapey's avatar

Do you need to initialise them to something?

My example was a concert = an 'event' that needs editing, the view passes the current choices to the livewire view so that it can set the select boxes to the existing state.

Snapey's avatar
Snapey
Best Answer
Level 122

well, I don't know what model contains the previous values, so you have to think about where this information comes from then pass it to the view

Antonella's avatar

the model is Task with the fields id, task and instance. I just didn't understand how to pass them to the view @snapey

Snapey's avatar

the model is Task

so pass Task to the view (probably as $task) then pass the task to the livewire component.

If you don't know how to pass a model to the view for editing then you are not ready for Livewire.

Please or to participate in this conversation.