Livewire component functions

So I have two Livewire components,
I want to emit the result of this function to another component;
public function loanCal()

{

    if ($staff = Staffrecord::where('staff_id', $this->staff_id)

        ->first()

    ) {

        $this->loanDeduction = [];

        foreach ($staff->loans as $loan) {

            foreach ($loan->loantype as $loan_name) {

                $this->loanDeduction[] = ['loan_amount' => $loan->loan_amount / $this->loanDuration, 'loantype' => $loan_name->name];

            }

        }

        return  $this->loanDeduction;

        **$this->emit('updated', $this->loanDeduction);**

}

On the Second Component
I have my Listener setup as so

protected $listeners = [‘updated’];

public function updated($LoanData)

{

    dd($LoanData);

}

But when ever the line of $this->emit(‘updated’, $this->loanDeduction); is executed nothing happens.

My Pay is how do I pass the result from a function from one livewire component to another

This two components have a parent-child relation? When you trying to access one of them the browser is refreshed?

No, the browser doesn’t refresh

ok, first I suppose that you have clear that the emit can’t be after the return…that never is gonna be parsed at least if-else statement is present. I suggest throws the event after catches this returned data or be sure that “return” is necessary. Example a function like

public function catched_return()
{
   $data = $this->funct_return();
   $this->emit('updated', $this->loanDeduction)
} 

Instead of this, if you are going to render the child component when the statements foreach is complete fire the event and after call the component for example a modal:

    foreach ($loan->loantype as $loan_name) {

       $this->loanDeduction[] = ['loan_amount' => $loan->loan_amount / $this->loanDuration, 'loantype' => $loan_name->name];
    }
}
$this->emit(‘updated’, $this->loanDeduction);
$this->dispatchBrowserEvent('openModalChildComponent');
}

I hope you understand my point and this can help you a little in your codding

Yeah this Fixed it…You are a Genius.

1 Like

hahaha, not my friend, just someone who shares what he learns. Good codding!