【发布时间】:2020-07-31 20:16:13
【问题描述】:
当我尝试将单个或多个记录从 false 更新为 true 时,我有一个布尔字段,但当尝试将其更新回 false 时,它仅适用于第一条记录,不能重复更新多条记录同时不刷新页面1-我处理请求的vue组件是这样的:
<template>
<div v-for="(channel, index) in items" :key="channel.id">
<a href="" @click.prevent="toggleVisibility(channel)">
<i v-if="channel.active" class="fas fa-stop default-stop" data-toggle="tooltip" title="Stop Channel">
</i>
<i v-else class="fas fa-play" data-toggle="tooltip" title="Start Channel"></i>
</a>
</div>
</template>
export default {
name: "Channels",
props: ['allChannels'],
data() {
return {
items: this.allChannels
}
},
methods: {
toggleVisibility(channel) {
axios[channel.active ? 'delete' : 'post'](`/visible-channels/${channel.name}`);
}
}
}
还有我的路线:
Route::post('/visible-channels/{channel}', 'ChannelsController@activate');
Route::delete('/visible-channels/{channel}', 'ChannelsController@deactivate');
我的控制器:
public function activate(Channel $channel, Request $request)
{
if ($request->method() == 'POST') {
$channel->update(['active' => true]);
}
return back();
}
public function deactivate(Channel $channel, Request $request)
{
if ($request->method() == 'DELETE') {
$channel->update(['active' => false]);
}
}
型号
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Cache;
class Channel extends Model
{
protected $guarded = [];
protected $casts = [
'active' => 'boolean',
];
protected static function boot()
{
parent::boot();
static::updating(function () {
return Cache::forget('activeChannels');
});
}
public function getRouteKeyName()
{
return 'name';
}
}
【问题讨论】: