Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/279.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添加额外数据_Php_Laravel - Fatal编程技术网

Php 如何使用同步方法laravel添加额外数据

Php 如何使用同步方法laravel添加额外数据,php,laravel,Php,Laravel,我使用的是Laravel,我有三个表:products、features和feature\u。第三个表有一个额外的列“value”,我希望在同步时为每个特性添加值 所以sync方法在没有值的情况下工作,但每当我想要发送值时,它就会给我错误 这是我的密码 if($request->get('feature')){ foreach($request->get('feature') as $featureName) { $feature =

我使用的是Laravel,我有三个表:products、features和feature\u。第三个表有一个额外的列“value”,我希望在同步时为每个特性添加值

所以sync方法在没有值的情况下工作,但每当我想要发送值时,它就会给我错误

这是我的密码

  if($request->get('feature')){
      foreach($request->get('feature') as $featureName)
      {
          $feature = Feature::find($featureName);
          if($feature)
          {
            $featureIds[] = $feature->id;
          }
      }

      $product->features()->sync($featureIds);
    }
这是我的错误

一般错误:1364字段“值”没有默认值(SQL:插入到
feature\u-product
feature\u-id
product\u-id
)值(11,99))


我知道这是因为我没有为此发送值,我想知道如何才能做到这一点

同步中缺少轴值,这就是为什么会出现此错误

if($request->get('feature')){
    $featureIds = array();
    foreach($request->get('feature') as $featureName)
    {
      $feature = Feature::find($featureName);

      if($feature){
        $featureIds[$feature->id] = ['value'=>'yourvalue'];
      }
    }

    $product->features()->sync($featureIds);
}
尝试使这种数组同步

$product->features()->sync( array( 
    1 => array( 'value' => 'xyz' ),
    2 => array( 'value' => 'abc' ),
    ...
));

您会遇到哪一个错误?文档中有一节“同步关联”:和的可能重复。