gpt4 book ai didi

mysql - Eloquent 和 mysql 转换为 bool 值不起作用

转载 作者:行者123 更新时间:2023-11-29 06:04:26 25 4
gpt4 key购买 nike

  $mm = new MyModel();
$mm->share_info = 'yes';
$mm->save();

型号:

class MyModel extends Model
{

public $table = 'mymodel';

const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';



public $fillable = [
'share_info',
];


protected $casts = [
'share_info' => 'boolean',
];

结果列,在mysql中是boolean tinyint。

数据库中的结果是 0 而不是 1。

为什么?

  $mm->share_info = true;

按预期工作

最佳答案

你也可以通过 laravel 魔术方法来设置/获取属性来实现这一点:

public function setShareInfoAttribute($value){
$this->attributes['share_info'] = (int) $value == 'yes';
}

public function getShareInfoAttribute(){
return $this->attributes['share_info'] ? 'yes' : 'no';
}

关于mysql - Eloquent 和 mysql 转换为 bool 值不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42629039/

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