Search code examples
laravel-livewire

How to call livewire method in template?


In the component of Laravel 8 / livewire 2 app I selected state(code_id) and I need to preview state and I try to make it with calling method of the component :

{{ getStateLabel($form['state_id']) }}

But I got error :

Call to undefined function getStateLabel() (View: myproject/resources/views/livewire/personal/new-item.blade.php)

In the component I have defined :

class NewItem extends Component
{

    public $statesSelectionArray = [];

    public function getStateLabel($state_id= 9)
    {
        foreach( $this->statesSelectionArray as $next_key=>$nextStateSelection ) {
            if($nextStateSelection['id'] == $state_id) {
                return $nextStateSelection['name'];
            }
        }
    }
    ...

}

uSIALLY WHEN IN TEMPLATE i CALL COMPONENT METHOD i USE SYNTAX :

wire:click="METHODnAME(2);

But which syntax have I to use in my case ?

Thanks in advance!


Solution

  • Change your code like this in your blade file

    {{ $this->getStateLabel($form['state_id']) }}