Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
punkestu committed Jan 27, 2025
1 parent 74bb036 commit dcbb2ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 38 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/Rotasi/CabangController.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ public function struktur()
public function strukturEdit()
{
$nodes = CabangNode::getTree();
dd($nodes);
$snodes = CabangNode::with('cabang')->get();
$cabangs = Cabang::all();
return view('rotasi.cabang.struktur-edit', ['nodes' => $nodes, 'snodes' => $snodes, 'cabangs' => $cabangs]);
Expand Down
56 changes: 18 additions & 38 deletions app/Models/CabangNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,47 +31,27 @@ public function children()

public static function getTree($rootId = null)
{
if ($rootId === null) {
$roots = CabangNode::with('cabang', 'children')->whereNull('root_id')->get();
if ($roots->count() == 0) {
return null;
}

return $roots->map(function ($root) {
return [
'id' => (string)$root->id,
'cabang_id' => $root->cabang_id,
'root_id' => $root->root_id,
'cabang' => $root->cabang,
'data' => [
'name' => $root->cabang->nama,
],
'children' => $root->children->count() > 0 ? $root->children->map(function () use ($root) {
return CabangNode::getTree($root->id);
}) : null,
];
});
}
$root = CabangNode::with('cabang', 'children')->where('root_id', $rootId)->first();
if (!$root) {
$roots = CabangNode::with('cabang', 'children')->where('root_id', $rootId)->get();
if ($roots->count() == 0) {
return null;
}

$result = [
'id' => (string)$root->id,
'cabang_id' => $root->cabang_id,
'root_id' => $root->root_id,
'cabang' => $root->cabang,
'data' => [
'name' => $root->cabang->nama,
],
'children' => $root->children->count() > 0 ? $root->children->map(function () use ($root) {
return CabangNode::getTree($root->id);
}) : null,
];
if ($result["children"] === null) {
unset($result["children"]);
}
$result = $roots->map(function ($root) {
$result = [
'id' => (string)$root->id,
'cabang_id' => $root->cabang_id,
'root_id' => $root->root_id,
'cabang' => $root->cabang,
'data' => [
'name' => $root->cabang->nama,
],
'children' => CabangNode::getTree($root->id),
];
if ($result["children"] == null) {
unset($result["children"]);
}
return $result;
});
return $result;
}
}

0 comments on commit dcbb2ba

Please sign in to comment.