Laravel 无法将关联键与livewire一起使用以从数组中删除输入字段

Laravel 无法将关联键与livewire一起使用以从数组中删除输入字段,laravel,laravel-livewire,Laravel,Laravel Livewire,我对livewire有一个不容易解释的问题,但我会尽力的。 基本上,我使用一个序列化记录中的键值对数组来创建一个动态表单。 用户应该能够添加和删除新字段。 问题在于remove方法,因为它似乎不接受键作为参数。 我能处理好吗? 这是livewire组件: class SetPositions extends Component { protected $listeners = ['updatePositions' => 'render']; public $position

我对livewire有一个不容易解释的问题,但我会尽力的。 基本上,我使用一个序列化记录中的键值对数组来创建一个动态表单。 用户应该能够添加和删除新字段。 问题在于remove方法,因为它似乎不接受键作为参数。 我能处理好吗? 这是livewire组件:

class SetPositions extends Component
{
    protected $listeners = ['updatePositions' => 'render'];
    public $positions = [];
    public $key;
    public $value;
    public function mount(){
        $this->positions = Settings::where('name', '=', 'PEOPLE_POSITIONS')->pluck('value')->first();
    }

    public function addPosition() {
        $this->positions[$this->key] = $this->value;
        $this->emit('updatePositions');
    }

    public function removePosition($param){
        dd($param);
    }

    
    <form method="post" wire:submit.prevent="update">
            @csrf
            @method('PUT')
            @foreach($this->positions as $key => $value)
            <div x-data="{ block: false}" class="sm:grid sm:grid-cols-3 sm:gap-4 sm:items-start sm:border-t sm:border-gray-200 sm:pt-5">
                <div x-show="!block" class="block text-sm font-medium text-gray-700 sm:mt-px sm:pt-2">
                    <!--<input :disabled="!block" value="" type="text" class="block w-full max-w-lg border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:max-w-xs sm:text-sm">-->
                    <label for="test" class="block w-full max-w-lg border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:max-w-xs sm:text-sm">
                        {{$key}}
                    </label>
                </div>
                <div x-show="!block" class="mt-1 sm:mt-0 sm:col-span-1">
                    <input wire:model="positions.{{$key}}" type="text" class="block w-full max-w-lg border-gray-300 rounded-md shadow-sm focus:ring-indigo-500 focus:border-indigo-500 sm:max-w-xs sm:text-sm">
                </div>
                <button wire:click="removePosition({{$positions[$key]}})" @click="block = !block" type="button">
                    <svg class="w-6 h-6" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" aria-hidden="true">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
                    </svg>
                </button>
            </div> 
            @endforeach

类集合位置扩展组件
{
受保护的$listeners=['updatePositions'=>'render'];
公共美元头寸=[];
公钥;
公帑价值;;
公共功能挂载(){
$this->positions=Settings::where('name','=','PEOPLE_positions')->pull('value')->first();
}
公共函数addPosition(){
$this->positions[$this->key]=$this->value;
$this->emit('updatePositions');
}
公共函数removePosition($param){
dd($param);
}
@csrf
@方法('PUT')
@foreach($this->定位为$key=>$value)
{{$key}}
@endforeach

请查看有关Pull方法的Laravel文档。现在,您可以将positions属性作为带有键值对的数组处理

你所说的“假定它似乎不接受键作为参数”是什么意思?$param是空的还是抛出异常?为什么要将
$positions[$key]
传递给
removePosition
方法?所要做的就是有效地传递
$value
。你最好做
removePosition({{$key}})
,然后从
$positions
property@frogeyedman$param返回一个空值array@JustCarty我已尝试同时传递$key和$value。在第一种情况下,该方法不会激发,在第二种情况下,它返回一个空数组。老实说,我认为您需要从头开始。最好从
$positions
来自控制器中的设置,然后将其传递到视图,然后将其传递到
foreach($Settings as$setting)
,然后将其传递到处理每个设置的Livewire组件,一次一个。这将更容易控制。