嵌套集保存到数据库
nested set save to database
我正在尝试使用 Laravel 应用程序实现 jqTree。
控制器获取数据和return视图:
public function index()
{
$categories = Category::get(['id', 'name', '_lft', '_rgt', 'parent_id'])->toTree();
return view('category.index', [
'categories' => $categories
]);
}
这是一个视图(javascript部分):
var data = {!! $categories !!};
$('#tree').tree({
data: data,
dragAndDrop: true,
onDragStop: handleMove,
});
function handleMove() {
var treeData = $('#tree').tree('toJson');
$.ajax({
url: 'category',
type: 'POST',
headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') },
data: { data: treeData },
});
}
这正确地构建了树,我可以拖放项目。但是,我想将重新排序的树保存回数据库。这是控制器中的 post 方法:
public function store(Request $request)
{
$data = $request->data;
$result = Category::rebuildTree($data);
...
}
调用 rebuildTree 方法后,出现此错误:
Type error: Argument 1 passed to Kalnoy\Nestedset\QueryBuilder::rebuildTree() must be of the type array, string given
我试过这个:
public function store(Request $request)
{
$data = $request->data;
$data = json_decode($request->data);
$array_data = (array) $data;
$result = Category::rebuildTree($data);
...
}
这可是 returns:
Cannot use object of type stdClass as array
如何获取传递的数据数组以便我可以使用 rebuildTree()
方法并更新数据库?
这是 dd($request->data):
的结果
[{"name":"Storage","id":3,"_lft":17,"_rgt":18,"parent_id":null,"is_open":true,
"children":[{"name":"Laptops","id":1,"_lft":1,"_rgt":10,"parent_id":null,"is_open":true,
"children":[{"name":"Monitors","id":2,"_lft":11,"_rgt":16,"parent_id":null,
"children":[{"name":"IPS","id":5,"_lft":14,"_rgt":15,"parent_id":2}]},
{"name":"Macbook","id":6,"_lft":2,"_rgt":3,"parent_id":1},
{"name":"Acer","id":7,"_lft":4,"_rgt":5,"parent_id":1},
{"name":"New Subcategory Name","id":8,"_lft":6,"_rgt":7,"parent_id":1},
{"name":"New Subcategory ame","id":9,"_lft":8,"_rgt":9,"parent_id":1}]}]},
{"name":"Memory","id":4,"_lft":19,"_rgt":20,"parent_id":null}]
此外,需要说明的是:我正在使用 jqTree
您在 $request->data;
中得到 json
您需要使用 json_decode()
以及第二个参数作为 assoc
的 true
在数组中对其进行解码
assoc
When TRUE, returned objects will be converted into associative
arrays.
public function store(Request $request)
{
$result = Category::rebuildTree(json_decode($request->data,True));
...
}
如果你需要在$data
中传递它,那么就这样传递。
$data = json_decode($request->data,True);
或者
foreach ($request->data as $value)
$array[] = $value->name;
然后像这样将此 $array
传递到您的查询中
$result = Category::rebuildTree($array);
我正在尝试使用 Laravel 应用程序实现 jqTree。
控制器获取数据和return视图:
public function index() { $categories = Category::get(['id', 'name', '_lft', '_rgt', 'parent_id'])->toTree(); return view('category.index', [ 'categories' => $categories ]); }
这是一个视图(javascript部分):
var data = {!! $categories !!}; $('#tree').tree({ data: data, dragAndDrop: true, onDragStop: handleMove, }); function handleMove() { var treeData = $('#tree').tree('toJson');
}$.ajax({ url: 'category', type: 'POST', headers: { 'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') }, data: { data: treeData }, });
这正确地构建了树,我可以拖放项目。但是,我想将重新排序的树保存回数据库。这是控制器中的 post 方法:
public function store(Request $request) { $data = $request->data; $result = Category::rebuildTree($data); ... }
调用 rebuildTree 方法后,出现此错误:
Type error: Argument 1 passed to Kalnoy\Nestedset\QueryBuilder::rebuildTree() must be of the type array, string given
我试过这个:
public function store(Request $request)
{
$data = $request->data;
$data = json_decode($request->data);
$array_data = (array) $data;
$result = Category::rebuildTree($data);
...
}
这可是 returns:
Cannot use object of type stdClass as array
如何获取传递的数据数组以便我可以使用 rebuildTree()
方法并更新数据库?
这是 dd($request->data):
[{"name":"Storage","id":3,"_lft":17,"_rgt":18,"parent_id":null,"is_open":true,
"children":[{"name":"Laptops","id":1,"_lft":1,"_rgt":10,"parent_id":null,"is_open":true,
"children":[{"name":"Monitors","id":2,"_lft":11,"_rgt":16,"parent_id":null,
"children":[{"name":"IPS","id":5,"_lft":14,"_rgt":15,"parent_id":2}]},
{"name":"Macbook","id":6,"_lft":2,"_rgt":3,"parent_id":1},
{"name":"Acer","id":7,"_lft":4,"_rgt":5,"parent_id":1},
{"name":"New Subcategory Name","id":8,"_lft":6,"_rgt":7,"parent_id":1},
{"name":"New Subcategory ame","id":9,"_lft":8,"_rgt":9,"parent_id":1}]}]},
{"name":"Memory","id":4,"_lft":19,"_rgt":20,"parent_id":null}]
此外,需要说明的是:我正在使用 jqTree
您在 $request->data;
中得到 json
您需要使用 json_decode()
以及第二个参数作为 assoc
true
在数组中对其进行解码
assoc
When TRUE, returned objects will be converted into associative arrays.
public function store(Request $request)
{
$result = Category::rebuildTree(json_decode($request->data,True));
...
}
如果你需要在$data
中传递它,那么就这样传递。
$data = json_decode($request->data,True);
或者
foreach ($request->data as $value)
$array[] = $value->name;
然后像这样将此 $array
传递到您的查询中
$result = Category::rebuildTree($array);