Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Rretzko's avatar
Level 15

Livewire 3 select box linked to array not firing updated* when changed

Hi Guys - I'm having a problem getting the database updated when a select form is changed on a Livewire 3 implementation. The select values are linked to an array. The process is working well and the changes are being recorded in the Livewire Form, but it is not triggering the updatedFormScores() in the parent livewire component & I can't see where I've gone wrong. Here's the relevant blade component:

 @foreach($form->factors AS $factor)
                @php
                    $start = $factor->best;
                    $end = $factor->worst;
                    $interval = $start > $end ? -$factor->interval_by : $factor->interval_by;
                @endphp
                <fieldset class="flex flex-row w-full">
                    <label for="{{ $form->scores[$factor->id] }}" class="w-1/6">{{$factor->factor}}</label>
                    <select wire:model.live="form.scores.{{ $factor->id }}"  wire:key="factor_{{ $factor->id }}">
                        @for($i=$start; (($start > $end) ? $i>=$end : $i<=$end ); $i=($i + $interval))
                            <option value="{{ $i }}">{{ $i }}</option>
                        @endfor
                    </select>
                </fieldset>
            @endforeach
                <div>
                    Total:  <span class="ml-2 font-semibold"  >{{ array_sum($form->scores) }}</span>
                </div>

When I change a select value, the Total value at the bottom adjusts accordingly, so I know the livewire linkage is there. The method in the parent component (AjudicationComponent) is as follows:

public function updatedFormScores()
    {
        $this->form->updateScores();
    }

which should call the AdjudicationForm's updateScores() method, but it's not firing at all (I checked this with a dd(LINE) command and it's not being pinged.

Your help is appreciated!

0 likes
2 replies
LaryAI's avatar
Level 58

To address the issue where the updatedFormScores() method is not being triggered in your Livewire component, you need to ensure that the method name matches the property being updated. In Livewire, the updated* lifecycle hooks are triggered based on the property name that changes.

Given your code, you're using wire:model.live="form.scores.{{ $factor->id }}". This means that the property being updated is form.scores, and Livewire expects a method named updatedFormScores to handle changes to this property.

Here are a few things to check and adjust:

  1. Ensure Correct Method Naming: The method name should match the property path. Since you're using form.scores, the method updatedFormScores is correctly named. However, ensure that there are no typos or case mismatches.

  2. Check for Nested Properties: If you want to handle updates to specific nested properties, you can use a more specific method name. For example, if you want to handle changes to a specific score, you could use:

    public function updatedFormScores($value, $key)
    {
        // $key will be the specific key that was updated, e.g., '1', '2', etc.
        // $value will be the new value of that key
        $this->form->updateScores();
    }
    
  3. Ensure Method is in the Correct Component: Make sure that the updatedFormScores method is in the same Livewire component class where the form.scores property is defined.

  4. Check for JavaScript Errors: Sometimes, JavaScript errors can prevent Livewire from functioning correctly. Open your browser's developer console and check for any errors.

  5. Livewire Version: Ensure that you are using the correct syntax for the version of Livewire you are using. The .live modifier is specific to Livewire 3, so ensure your setup is compatible.

  6. Debugging: Add a dd() or log statement at the beginning of the updatedFormScores method to confirm if it's being called at all.

If all else fails, try simplifying the problem by creating a minimal example to isolate the issue. This can help identify if there's something specific in your setup causing the problem.

Chingy's avatar

I believe that the function doesn't get executed since the correct key is form.scores.1,2,3... that would

public function updatedFormScores1()

which that would be tedious. Have you tried if the updatedForm works?

Please or to participate in this conversation.