布尔型数据存储不了
Customers 表中添加布尔型字段 is_client
Schema::table('customers', function (Blueprint $table) {
$table->boolean('is_client')->default(false)->nullable()->after('rank');
});
Customer 模型字段中:
protected $guarded = [];
protected $casts = [
'is_client' => 'boolean',
];
控制器中为什么存储不了
报错:
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ‘true’ for column ‘is_client’ at row 1
把 casts 去掉就行了,布尔值在数据库也就 0 和 1 的存在,你现在转成字符串 true 了
@Hanson 并不需要去掉
casts
,我一直都是用的casts
转Boolean
。把你的sql打印出来给我看看
这个字段类型从数据库拿出来默认就是 true 和 false 了
提示的也很明确,在转换的过程中,需要 int 类型的参数,给的是个 bool 类型的参数