Php 如何在Laravel的Eloquent中设置mysql位数据类型?(拉威尔5.4)
我只有一个操作来停用用户的资源。该操作使用一个位值来激活/停用资源,但我很惊讶我无法按预期设置位值。该值始终设置为1,即创建资源时的默认值Php 如何在Laravel的Eloquent中设置mysql位数据类型?(拉威尔5.4),php,eloquent,laravel-5.4,Php,Eloquent,Laravel 5.4,我只有一个操作来停用用户的资源。该操作使用一个位值来激活/停用资源,但我很惊讶我无法按预期设置位值。该值始终设置为1,即创建资源时的默认值 public function deactivate(Request $request) { $resourceId= $request->get('resourceId'); $resource= \App\Resource::find($resourceId);
public function deactivate(Request $request)
{
$resourceId= $request->get('resourceId');
$resource= \App\Resource::find($resourceId);
//I've tried the two statements below separetely
$resource->active= false; //didn't work
$resource->active= 0; //didn't work
//I performed the test below with another column and it worked
//so the problem isn't my model.
$resource->anotherProperty = 10;
$resource->save();
}
尽管我不认为问题出在我的模型上,但我使用的是数据库优先的方法,问题可能出在我创建的模型上
class Resource extends Model
{
protected $table = 'resource';
protected $primaryKey = 'resource_id';
protected $fillable = array( 'announcer_id', 'announcer_type', 'data', 'active');
public $timestamps = false;
protected $connection = 'custom_connection';
}
几分钟后更新
CREATE TABLE `resource` (
`resource_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`announcer_id` int(10) unsigned NOT NULL,
`announcer_type` tinyint(3) unsigned NOT NULL,
`data` date DEFAULT NULL,
`active` bit(1) DEFAULT b'1',
PRIMARY KEY (`resource_id`)
) ENGINE=InnoDB AUTO_INCREMENT=583 DEFAULT CHARSET=latin1;
经过研究,我发现MySQL有一个布尔数据类型,存储为tinyint1 我改变了我的桌子
alter table resource modify column active boolean not null;
现在在我的控制器上
$resource->active= false; //works
显示表架构