sync() 和attach() 不能一起工作?

问题描述 投票:0回答:2

首先在我的控制器中,我为附加字段附加这样的 id 数组:

$property->features()->attach($additional_ids);

然后我使用

sync()
因为我有复选框:

 $property->features()->sync($features);

但问题是,当我有这个时,我的

attach()
就不起作用了。有什么建议我该如何解决这个问题吗?当我删除这个
sync()
时,我的附件正在工作。所以我的想法是,我有功能的复选框,并且我有一些附加功能的输入字段。我将此 id 存储在数据透视表中。

php laravel laravel-5
2个回答
3
投票

sync()
将覆盖
attach()
所做的所有更改。所以,只需合并数组并使用
sync()

$property->features()->sync(array_merge($features, $additional_ids));

0
投票

使用这个:

$property->features()->sync($features, false);

这将同步而不分离

或者您也可以致电:

$property->features()->syncWithoutDetaching($features)

这也会做同样的事情。

© www.soinside.com 2019 - 2024. All rights reserved.