gpt4 book ai didi

laravel - 无法通过Vue和Laravel更新表中的 bool 值

转载 作者:行者123 更新时间:2023-12-03 06:45:24 30 4
gpt4 key购买 nike

当我尝试将单个或多个记录的值从false更新为true时,我有一个 bool(boolean) 字段,但是当尝试将其更新为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');

我的 Controller :
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';
}
}

最佳答案

由于laravel在数据库中将boolean分别存储为10,因此您应该在模型中将active属性设置为boolean
这是因为laravel将false视为字符串,因此当您将active设置为false时,会将其与'false' == true进行比较,即true,因此将1存储在数据库中。

class Channel extends Model
{
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'active' => 'boolean',
];
}

关于laravel - 无法通过Vue和Laravel更新表中的 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61286699/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com