Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.6k views
in Technique[技术] by (71.8m points)

Laravel Livewire: Setting model property does not refresh the view

I don't know if it is a bug or just a misusage, so I'm asking here...

on my view :

<div>
    <button wire:click="edit(1)"/>
    @if ($updateMode)
    Editing id {{ $productType->id }} / {{ $productType->name }}
    <input wire:model="productType.id"/> <!-- renders nothing -->
    <input wire:model="productType.name"/> <!-- renders the Product name correctly -->
    @endif
</div>

on my component :


use AppModelsProductType;
use LivewireComponent;

class ProductTypes extends Component
{
    public ProductType $productType;
    public bool $updateMode;

    public function mount()
    {
        $this->productType = new ProductType();
        $this->updateMode = false;
        $this->productTypes = ProductType::all();
    }

    public function edit($id)
    {
        $this->productType = $this->productTypes->find($id);
        // dd($this->productType->id);
        $this->updateMode = true;
    }

    /** ... */
}

You see there is a dd($this->productType->id) which displays correct information.

But on the view, it always displays the last ProductType it has created.

By using <input wire:model="productType.name"/>, it displays the right info in the input field, but when using {{ $product->name }} is does not. Another note is by using <input wire:model="productType.id"/>it displays empty field, no matter what.

Any idea how to solve this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I feel so dumb.

In the view, just replace: {{ $product->name }}

by {{ $this->product->name }}


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...