Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
Livewire property is null when value updated through php

I have a very simple livewire component

class Test extends Component
{

    public $test = "test";

    public function submit()
    {
        dd($this->test);
    }

    public function render()
    {
        return view('livewire.test');
    }
}

and view

<div>
    <form wire:submit.prevent="submit" method="post">
        <input type="text" wire:model="test" id="test">
        <button type="button" id="ok">ok</button>
        <button type="submit">submit</button>
    </form>
    <script>
        const button = document.getElementById('ok');
        button.addEventListener('click', function() {
            const input = document.getElementById('test');
            input.value = "Test";
        });
    </script>
</div>

I simplified the code for illustrative purposes to show that JavaScript changes a value.

When I click ok what changes the value from test to Test and then submit, I get test shown instead of Test.

I except to see Test. What am I doing wrong?

It seems that the Livewire Component is not recognising the change from the Javascript code, as the following function doesn't fire either:

public function updatedTest()
{
   dd("Fired");
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can't manipulate properties in that manner with Livewire. The preferred way is to use the AlpineJS entangle method which specifically caters for sharing state between Livewire and your client.

That being said, you can achieve what you're after without using AlpineJS. Replacing your existing JavaScript with the below should get you the result you're after.

<script>
    document.addEventListener('livewire:load', (evt) => {
        const button = document.getElementById('ok');
        button.addEventListener('click', function() {
            document.getElementById('test').value = @this.test = 'Test';
        });
    })
</script>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!