laravel-livewire - 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]);
}
解决方案
创建一个新的属性来存储在 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');
}
}
推荐阅读
- django - DRF 模型序列化器外键返回对象,而不是 ID
- php - 使用 PHP 和 Mysql 将空字符串更改为 Null 或“None”ssp 类
- docker - 如何在 Heroku 上创建/迁移 Elixir Phoenix Docker 镜像?
- c# - 二进制搜索字符串数组未找到搜索的字符串C#
- numpy - 广播相同形状的多维数组索引
- java - 给用户输入以将特定的 int 或 char 放在数组中的某个位置?
- git - 是否可以在 git diff 命令之前应用代码格式
- javascript - 将文本标签添加到 D3 中的每个堆叠条堆栈
- javascript - JavaScript - 设置元素的值不起作用
- excel - 根据单元格值选择工作表