its not weird because the model is fetched from the database on each render
Dec 2, 2024
3
Level 1
Eloquent model does not persist between Livewire updates
Livewire component does not track Eloquent model between updates. Super simple example below:
TestComponent.php
<?php
namespace App\Livewire;
use App\Models\Individual;
use Livewire\Component;
class TestComponent extends Component
{
public Individual $individual;
public function mount() {
$this->individual = new Individual();
$this->individual->first_name = 'joe';
$this->individual->last_name = 'schmoe';
}
public function updateFunction() {
// Do Nothing
}
public function render()
{
return view('livewire.test-component');
}
}
test-component.blade.php
<div>
<div>{{ $this->individual->first_name }}</div>
<button wire:click="updateFunction()">Click me</button>
</div>
On first render, the name, 'joe', is displayed. After clicking on the button, the name disappears because $individual->first_name is wiped. When I get the class of $individual, it's still shows an Eloquent model.
This is weird, because https://livewire.laravel.com/docs/properties#supported-property-types says that Illuminate\Database\Eloquent\Model is a supported property type.
Help please!
Level 122
1 like
Please or to participate in this conversation.