2

Variation我有一个整页的 livewire 组件,我正在使用单个模态进行 crud,我正在为产品创建变体,并且变体具有多个值,所以在这种情况下,我在和之间创建了一对多的关系VariationValue。当我创建没有问题但是当我想更新有问题时,我的多个输入的index值在输入内部没有正确显示,如果我这样做$value->value,我会在输入中看到正确的值,但我无法更新或删除它们,直到我没有id.

我希望该用户可以根据需要更新和删除并添加更多字段,但是我的问题是我无法在输入中显示正确的值并且无法id从中删除要删除的内容db,我正在尝试使用$editModelikewire:model.defer="{{ $editMode ? $values[$key]->value : 'values.'.$key }}"但它不起作用,它只是wire:model.defer="value"在我检查元素时向我显示里面的值。我如何为更新和删除新值执行此操作,我搜索了很多但没有找到结果,在图像下方您可以看到[object Object]这些输入内部没有显示正确的值。

$editMode = true它向我展示这样

在此处输入图像描述

在我的模态刀片中

<div class="col-12">
   <fieldset>
     <label>Variation values *</label>
     @foreach ($moreValues as $key => $value)
        <div class="{{ $loop->last ? '' : 'mb-2' }}" wire:key="{{ $key }}">

          <div class="input-group">
            <input type="text" 
                   wire:model.defer="values.{{ $key }}" 
                   class="form-control"                                       
                   placeholder="Variation value">

            <div class="input-group-append">
             @if ($loop->index === 0)
               <button wire:click.prevent="addRowForValue" 
                       class="btn btn-primary font-size-large" type="button">
                    +
               </button>
             @else
               <button wire:click.prevent="removeRowForValue({{ $key }})"
                       class="btn btn-danger font-size-large" type="button">
                    -
               </button>
             @endif
           </div>
         </div>

        @error("values.{{ $key }}")
           <span class="text-danger font-size-base">{{ $message }}</span>
        @enderror
      </div>
    @endforeach
  </fieldset>
</div>

在我的组件中

<?php

namespace App\Http\Livewire\Products\Variations;

use App\Models\Variation;
use Livewire\Component;

class Index extends Component
{

    public $moreValues = [0];

    public $editMode = false;

    public $name, $values = [];

    protected $rules = [
        'name' => 'required|string',
        'values.*' => 'required|string',
    ];

    public function addRowForValue()
    {
        $this->moreValues[] += 1;
    }

    public function removeRowForValue($key)
    {
        unset($this->moreValues[$key]);
    }

    public function store()
    {
        $this->validate();

        $variation = Variation::create([
            'name' => $this->name
        ]);

        foreach ($this->values as $key => $value) {
            $variation->values()->create([
                'value' => $this->values[$key]
            ]);
        }

        session()->flash('message', 'Unit create successfully.');

        $this->clearForm();
    }

    /**
     * @param $id
     */
    public function edit($id)
    {
        $this->editMode = true;

        $this->moreValues = [0];

        $this->resetErrorBag();

        $this->clearForm();

        // fill the form here with data
        $variation = Variation::query()->findOrFail($id);

        $this->name = $variation->name;

        $this->values = [];

        foreach ($variation->values as $key => $value) {

            // incrementing input by value
            $this->moreValues[$key] = $value;

            // showing value their own field
            $this->values[$key] = $value;
        }

        $this->emit('openModal');
    }

    protected function clearForm()
    {
        $this->name = '';
        $this->values = '';
    }

    public function render()
    {
        $variations = Variation::with('values')->latest('id')->get();

        return view('livewire.products.variations.index', compact('variations'))
            ->extends('layouts.app');
    }
}

4

0 回答 0