1
  $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。

结果在 DB 中为 0 而不是 1。

为什么?

  $mm->share_info = true;

确实按预期工作

4

1 回答 1

3

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

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

public function getShareInfoAttribute(){
    return $this->attributes['share_info'] ? 'yes' : 'no';
}
于 2017-03-23T08:23:08.767 回答