问题描述
限时送ChatGPT账号..我正在尝试将数据保存在数据透视表中,其中包含一个名为 data 的额外字段.
当我保存时,我有这个数组:
<预><代码> [5 =>文件"4 =>图片"3 =>标签"1 =>大拇指"]我的桌子是这样的:
project_idoption_id姓名上面显示的 id 是指 option_id 和要在数据库中命名的字符串.
当我尝试像这样使用同步时:$project->options()->sync($data);
$data 是上面显示的数组
我收到一个错误,那就是它试图用文件"保存 option_id.
以下是我构建用于同步的数据的方法:
我正在尝试获得您的建议,但不知道如何实现:
这是我建立阵列的方法:
foreach($request->input('option_id') as $id) {$option['option_id'][] = $id;$option['data'][] = $request->input('data')[$id];}$data = array_combine($option['option_id'], $option['data']);
解决方案 这在手册中有介绍:
<块引用>同步时添加枢轴数据
您还可以将其他数据透视表值与给定的 ID 关联:
$user->roles()->sync(array(1 => array('expires' => true)));
在您的示例中,您必须将数组更改为如下所示,但我相信这会转化为:
$data = [5 =>['姓名' =>文件"],4 =>['姓名' =>图片"],3 =>['姓名' =>标签"],1 =>['姓名' =>大拇指"],];$project->options()->sync($data);
我相信您可能还需要修改 Project
模型与 Options
模型的关联方式:
//文件:app/model/Project.php公共功能选项(){return $this->belongsToMany('Option')->withPivot('name');}
这也在链接到的手册页中注明:
<块引用>默认情况下,只有键会出现在枢轴对象上.如果您的数据透视表包含额外的属性,您必须在定义关系时指定它们.
更新
尝试像这样创建您的 $data
数组:
$data = [];foreach($request->input('option_id') as $id) {$data[$id] = [ 'name' =>$request->input('data')[$id]];}
Im trying to save data inside a pivot table with an extra field called data.
when i save i have this array:
[
5 => "files"
4 => "pictures"
3 => "tags"
1 => "thumbs"
]
My table looks like this:
project_id option_id nameThe ids shown above refer to option_id and the string to name inside the database.
When i try to use sync like this: $project->options()->sync($data);
$data is the array shown above
Im getting a error thats its trying to save the option_id with "files".
Here is how i build up the data that i use for sync:
Im trying to get what you suggested but dont know how to achieve it:
here is how i build up the array:
foreach($request->input('option_id') as $id) {
$option['option_id'][] = $id;
$option['data'][] = $request->input('data')[$id];
}
$data = array_combine($option['option_id'], $option['data']);
解决方案
This is covered in the manual:
Adding Pivot Data When Syncing
You may also associate other pivot table values with the given IDs:
$user->roles()->sync(array(1 => array('expires' => true)));
In your example, you would have to change your array to look something like below but I believe this would translate to:
$data = [
5 => [ 'name' => "files" ],
4 => [ 'name' => "pictures" ],
3 => [ 'name' => "tags" ],
1 => [ 'name' => "thumbs" ],
];
$project->options()->sync($data);
I believe you may also need to modify how your Project
model relates itself to your Options
model:
// File: app/model/Project.php
public function options()
{
return $this->belongsToMany('Option')->withPivot('name');
}
This is also noted in the linked-to manual page:
By default, only the keys will be present on the pivot object. If your pivot table contains extra attributes, you must specify them when defining the relationship.
Update
Try creating your $data
array like this:
$data = [];
foreach($request->input('option_id') as $id) {
$data[$id] = [ 'name' => $request->input('data')[$id] ];
}
这篇关于Laravel:与数组的额外字段同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论