【问题标题】:Laravel Livewire: Setting model property does not refresh the viewLaravel Livewire:设置模型属性不会刷新视图
【发布时间】:2021-04-17 16:53:55
【问题描述】:

我不知道这是一个错误还是只是一个误用,所以我在这里问......

在我看来:

<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>

在我的组件上:


use App\Models\ProductType;
use Livewire\Component;

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;
    }

    /** ... */
}

您看到有一个dd($this-&gt;productType-&gt;id) 显示正确的信息。

但在视图上,它始终显示它创建的最后一个 ProductType。

通过使用&lt;input wire:model="productType.name"/&gt;,它会在输入字段中显示正确的信息,但使用{{ $product-&gt;name }} 时不会。 另一个注意事项是使用&lt;input wire:model="productType.id"/&gt;它无论如何都会显示空字段。

知道如何解决这个问题吗?

【问题讨论】:

    标签: laravel laravel-livewire


    【解决方案1】:

    我觉得自己很笨。

    在视图中,只需替换: {{ $product-&gt;name }}

    {{ $this-&gt;product-&gt;name }}

    【讨论】:

      猜你喜欢
      • 2021-09-10
      • 1970-01-01
      • 2021-01-11
      • 2021-03-07
      • 1970-01-01
      • 1970-01-01
      • 2021-07-27
      • 1970-01-01
      • 2021-01-23
      相关资源
      最近更新 更多