事件发生后 Livewire 未使用数据库中的值进行更新
Livewire not updating with values from db after event
我正在尝试从按钮中选择所有长度,然后可以返回并更改不同站点的特定值。现在两者都有效,但是当我现在使用按钮时出现错误
Call to a member function refresh() on array
I tried using update/updating but it doesn't bring the values from the database.
<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');
}
}
我正在尝试从按钮中选择所有长度,然后可以返回并更改不同站点的特定值。现在两者都有效,但是当我现在使用按钮时出现错误
Call to a member function refresh() on array I tried using update/updating but it doesn't bring the values from the database.
<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');
}
}