首页 > 解决方案 > Livewire 在事件发生后未使用 db 中的值进行更新

问题描述

我试图从按钮中选择所有长度,然后可以返回并更改不同站点的特定值。现在两者都可以工作,但是当我现在使用按钮时出现错误

调用数组上的成员函数 refresh() 我尝试使用更新/更新,但它不会从数据库中获取值。

<div>
       <button wire:click="$emit('sec60')">60 Sec</button>
       <button wire:click="$emit('sec30')">30 Sec</button>

<table class="table-auto w-full mb-6">
          <thead>
            <tr>
             <th  style="cursor: pointer;" class="px-4 py-2">Spot Length @include('partials.sort-icon',['field'=>'spot_length'])</th>
            </tr>
          </thead>
          <tbody>
   @foreach($selected_stations as $key => $selected_station)
              <tr>
                <td class="border px-4 py-2">
                    <select  wire:model="spot_length.{{$selected_station->id}}" class="block appearance-none w-full bg-gray-200 border border-gray-200 text-gray-700 py-3 px-4 pr-8 rounded leading-tight focus:outline-none focus:bg-white focus:border-gray-500" id="grid-state" value="{{$current_workbook->stations()->find($selected_station->id)->pivot->spot_length}}">
                        <option value="">Select One</option> 
                        <option value="30">:30</option>
                        <option value="60">:60</option>
                    </select>
                </td>
              </tr>
            @endforeach
          </tbody>
        </table>
</div>
public function mount($client, $workbook){
        
        $this->selected_stations= $workbook->stations;
        $this->current_workbook = Workbook::find($this->workbook_id);
        $this->spot_length = $this->selected_stations->pluck('pivot.spot_length', 'id')->all();
    }

    public function sec30(){
        foreach($this->selected_stations as $key => $selected_station){
            $this->current_workbook->stations()->updateExistingPivot($selected_station->id, ['spot_length'=>30]);
        }
        $this->spot_length->refresh();
    }

    public function sec60(){
        foreach($this->selected_stations as $key => $selected_station){
            $this->current_workbook->stations()->updateExistingPivot($selected_station->id, ['spot_length'=>60]);
        }
        $this->spot_length->refresh();
    }
    public function updatedSpot($value, $key){
        $this->current_workbook->stations()->updateExistingPivot($key, ['spot'=>$value]);
    }

标签: laravel-livewire

解决方案


创建一个新的属性来存储在 mount 中传递的 $workbook 的 id。

public $workbook_id;

public function mount($client, $workbook){
  $this->workbook_id = $workbook->id;
  $this->selected_stations= $workbook->stations;
  $this->current_workbook = Workbook::find($this->workbook_id);
  $this->spot_length = $this->selected_stations->pluck('pivot.spot_length', 'id')->all();
}

public function sec30(){
   foreach ($this->selected_stations as $key => $selected_station) {                           
      $this->current_workbook->stations()->updateExistingPivot($selected_station->id,  ['spot_length'=>30]);
   }
   $this->refreshSpotLenghtProperty();
}

public function refreshSpotLenghtProperty()
{
   $workbook = WorkBook::where('id',$this->workbook_id)->first();
   if($workbook) {
      $this->selected_stations = $workbook->stations;
      $this->spot_length = $this->selected_stations->pluck('pivot.spot_length', 'id');
   }
}

推荐阅读