Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 5.4:雄辩的sync()不使用模型的变异体或属性转换?_Php_Mysql_Laravel_Eloquent_Laravel Eloquent - Fatal编程技术网

Php Laravel 5.4:雄辩的sync()不使用模型的变异体或属性转换?

Php Laravel 5.4:雄辩的sync()不使用模型的变异体或属性转换?,php,mysql,laravel,eloquent,laravel-eloquent,Php,Mysql,Laravel,Eloquent,Laravel Eloquent,我有一个带有“修改”属性的模型。 模型使用set属性。这是因为存储被保留为布尔值,但该属性通过选择进行管理,因为可用的FE选项有三种状态:[无|修改|访问],其中不存储“无” 存储如下所示: +-------------+------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------

我有一个带有“修改”属性的模型。 模型使用set属性。这是因为存储被保留为布尔值,但该属性通过选择进行管理,因为可用的FE选项有三种状态:[无|修改|访问],其中不存储“无”

存储如下所示:

+-------------+------------------+------+-----+---------+-------+
| Field       | Type             | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+-------+
| prop_id     | int(10) unsigned | NO   | PRI | NULL    |       |
| modify      | tinyint(1)       | NO   |     | 1       |       |
+-------------+------------------+------+-----+---------+-------+
该模型使用以下mutator方法将“modify”转换为true

public function setModifyAttribute($value)
{
    $this->attributes['modify'] = ($value === "modify");
}
传递用于同步的典型集合如下所示:

$properties = 
Collection {
  #items: array [
    1 => array [
      "modify" => "modify"
    ]
    7 => array [
      "modify" => "access"
    ]
  ]
}
$this->properties()->sync($properties);
然后,我调用属性的同步,如下所示:

$properties = 
Collection {
  #items: array [
    1 => array [
      "modify" => "modify"
    ]
    7 => array [
      "modify" => "access"
    ]
  ]
}
$this->properties()->sync($properties);
但是,无法从同步访问setModifyAttribute

或者,使用值“true”和“false”sync似乎会忽略属性转换提示

protected $casts = ['modify' => 'boolean'];

我应该在集合上运行某种闭包吗?我更喜欢使用以模型为中心的流程。

从laravel文档中,方法setXXXAttribute如下所示

public function setFirstNameAttribute($value)
    {
        $this->attributes['first_name'] = strtolower($value);
    }
因此,我想应该修改您的代码:

public function setModifyAttribute($value){
($value==="Modify"):$this->attributes['modify']=1?$this->attributes['modify']=0;

}

可能是错误的,祝你好运,你是对的,我的方法错了,但是sync不会调用属性。我想,sync方法是用来同步provide表中的属性的。而你的模型关系是多对多的?你找到答案了吗?@Danialdezjulli,老实说-没有。我只是在最后解决了这个问题。