Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.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
Arrays 在数据库中插入多对多关系可以调用null上的成员函数sync()_Arrays_Laravel_Many To Many - Fatal编程技术网

Arrays 在数据库中插入多对多关系可以调用null上的成员函数sync()

Arrays 在数据库中插入多对多关系可以调用null上的成员函数sync(),arrays,laravel,many-to-many,Arrays,Laravel,Many To Many,我有一个多对多关系 报价模型 public function products(){ return $this->belongsToMany(Product::class); } public function quotes(){ return $this->belongsToMany(Quote::class); } 产品型号 public function products(){ return $this->belongsToMany(Produ

我有一个
多对多
关系

报价模型

public function products(){
    return $this->belongsToMany(Product::class);
}
public function quotes(){
    return $this->belongsToMany(Quote::class);
}
产品型号

public function products(){
    return $this->belongsToMany(Product::class);
}
public function quotes(){
    return $this->belongsToMany(Quote::class);
}
我从输入表单接收到我的
控制器的详细信息。结果如下:

array:39 [▼ 
"_token" => "NgcHxCjpGUe1ot8nJr4Z8VFuA3DG9VprKWqRu5yk"
"quote_id" => "52" 
20 => "0"
10 => "0"
11 => "0"
12 => "0"
13 => "0"
14 => "0"
15 => "0"
16 => "0"
17 => "0"
“quote\u id”
是指在上述输入表单之前的步骤中创建的报价。
quote_id
从表单中的隐藏输入字段传递给控制器。在上面的数组中,
$key
表示
产品id
$value
表示产品数量

我正在尝试将数据插入
产品

Schema::create('product_quote', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('product_id');
        $table->unsignedBigInteger('quote_id');
        $table->integer('qty');
        $table->timestamps();
    });
以下是我的代码:

$data = $request->toArray();
$quote_id = $data['quote_id'];
    unset($data['quote_id']);
    $quote = Quote::query()->findOrFail($quote_id);
    foreach (array($data) as $key => $value){
        $quote->products->sync($data);
        $quote->save();
    }
之后,我仍然需要添加产品数量,但我得到了一个错误:调用null上的成员函数sync()


我做错了什么?

您需要使用
产品关系,而不是集合

$quote->products()->sync($data);

这是对集合与关系的误解

在Laravel中,如果您的案例中有关系产品,请访问您将调用的关系

$quote->products(); // returns belongsToMany relation query
要访问集合,请将其作为属性访问

$quote->products; // returns a collection with the products
在您的情况下,您将作为一个集合访问它。如果您得到一个空错误,那么其他一些可能是错误的,但是首先将其更改为以下内容

其次,要设置透视字段,请在同步调用上使用键值数组,使用结构中的键和值。您可能还应该取消设置令牌。相反,我使用except对其进行过滤。更好的方法是在表单请求上使用validated

您需要调整数据以适应同步调用上的预期键值结构,同步也应该只调用一个,因为它在每个调用之间同步数据

$data = $request->toArray();

$quote = Quote::findOrFail($data['quote_id']); // calling query() is unnecessary

$products = collect($request->except(['_token', 'quote_id']))->
    ->mapWithKeys(function($item, $key) {
        return [$key => ['qty' => $item]];
    })->all();

$quote->products()->sync($products);

此解决方案使用mapWithKeys,它允许您控制集合中的键和项,这是一种不常使用的集合方法。闭包的返回格式应为
return['key'=>'item']而不是
返回“项目”

首先我要感谢你。我现在至少在
product\u quote
表中得到一个条目。然而,这只是一个条目。它在该条目上确实获得了以下正确信息。1)
产品标识
2)产品标识
3)数量
但除了只得到一个条目外,
id
不正确,第一个条目是
id=33
,与进入数据库的产品标识相同。。第二个条目是
id=66
,第三个条目是
id=132
。我对变量$data执行了
dd()
,33的
product\u id
是我从输入表单中得到的最后一个条目。有什么建议吗?哦,等等:)同步删除通话中的所有条目。将更新答案。非常感谢。成功了。在从表单接收数据后,我确实对
if语句应用了一些逻辑,这些语句没有显示在
表中。但我假设这是因为它们不是$request的一部分,我需要将它们添加到数组中。我真希望你是我的朋友:)我4点坐在这里想办法解决这个问题,你救了我。如果你有困难,请在这里发表评论并链接到一个问题,我可以随时查看:)谢谢你的回答。我会调查你说的话,这样我才能理解,以后不会再犯同样的错误。