Laravel 具有轴心和嵌套子项的Eloquent sync()属于
我想解决这个问题。我得到了这样一个json:Laravel 具有轴心和嵌套子项的Eloquent sync()属于,laravel,eloquent,laravel-collection,Laravel,Eloquent,Laravel Collection,我想解决这个问题。我得到了这样一个json: [ { "id": 2, "title": "One", "parent": {}, "children": [ { "id": 3, "title": "One One", "parent": { "id": 2, "title": "One" }, "children": [],
[
{
"id": 2,
"title": "One",
"parent": {},
"children": [
{
"id": 3,
"title": "One One",
"parent": {
"id": 2,
"title": "One"
},
"children": [],
"value": 1
},
{
"id": 4,
"title": "One Two",
"parent": {
"id": 2,
"title": "One"
},
"children": [],
"value": 2
}
],
"value": 3
},
{
"id": 5,
"title": "Three",
"value": 3
}
]
/*Save items*/
$items
= collect($request->input('data.items'))->mapWithKeys(function (
$item
) {
$traverse = function ($item) use (&$traverse) {
if (array_key_exists('value', $item)) {
$value = $item['value'];
} else {
$value = null;
}
foreach ($item['children'] as $child) {
$child = $traverse($child);
}
$children = ($item['children']);
return [
$item['id'] => compact('value', 'children'),
];
};
$item = $traverse($item);
如您所见,每个项目都可以有一个子项,也可以有一个子项,也可以有一个子项,以此类推。它是嵌套的
现在,我想将这些项和每个子项保存在与pivot的归属关系中,在本例中为:value。如果要使用同步,我必须在控制器中准备所有项目:
<?php
/*Save $items*/
$items
= collect($request->input('data.items'))->mapWithKeys(function (
$item
) {
if (array_key_exists('value', $item)) {
$value = $item['value'];
} else {
$value = null;
}
return [
$item['id'] => compact('value'),
];
});
$user->items()->sync($items);
但这并不可行
因此,我想要的是:
将每个项目保存为具有pivot值的用户的归属关系
使用pivot值递归保存所有子项
首先我想说的是,您的数据实际上是JSON,而不是数组。所以这里我写了一些代码,你们可以看到我解码了,从循环函数开始得到了一个数组
protected $result = [];
protected function deepDiveIntoNextLevel(array $array) {
foreach ($array as $item) {
$this->result[] = $item['value'];
if(!empty($item['children'])) {
$this->deepDiveIntoNextLevel($item['children']);
}
}
}
public function loop()
{
$json_array = '[
{
"id": 2,
"title": "One",
"parent": {},
"children": [
{
"id": 3,
"title": "One One",
"parent": {
"id": 2,
"title": "One"
},
"children": [],
"value": 1
},
{
"id": 4,
"title": "One Two",
"parent": {
"id": 2,
"title": "One"
},
"children": [],
"value": 2
}
],
"value": 3
},
{
"id": 5,
"title": "Three",
"value": 3
}
]';
$initial_array = json_decode($json_array, true);
$this->deepDiveIntoNextLevel($initial_array);
$result = $this->result;
// As output you may get an array, where you can have duplicates
sort($result);
// Or it can be not ordered
$relations_ids_odered = array_unique($result);
}
据我所知,您希望将所有项值收集到一个数组中,该数组将用于保存一个用户的所有关系。因此,您可以让输出数组看到循环函数的底部
protected $result = [];
protected function deepDiveIntoNextLevel(array $array) {
foreach ($array as $item) {
$this->result[] = $item['value'];
if(!empty($item['children'])) {
$this->deepDiveIntoNextLevel($item['children']);
}
}
}
public function loop()
{
$json_array = '[
{
"id": 2,
"title": "One",
"parent": {},
"children": [
{
"id": 3,
"title": "One One",
"parent": {
"id": 2,
"title": "One"
},
"children": [],
"value": 1
},
{
"id": 4,
"title": "One Two",
"parent": {
"id": 2,
"title": "One"
},
"children": [],
"value": 2
}
],
"value": 3
},
{
"id": 5,
"title": "Three",
"value": 3
}
]';
$initial_array = json_decode($json_array, true);
$this->deepDiveIntoNextLevel($initial_array);
$result = $this->result;
// As output you may get an array, where you can have duplicates
sort($result);
// Or it can be not ordered
$relations_ids_odered = array_unique($result);
}
结果你想要什么?请写下这一点,以便那些愿意提供帮助的人更容易阅读;对不起,我添加了丢失的信息,