Browse Source

add childern

pull/1/head
farhantock 12 months ago
parent
commit
215142ec38
  1. 36
      app/Http/Controllers/DivisiController.php

36
app/Http/Controllers/DivisiController.php

@ -8,7 +8,8 @@ use App\Models\Divisi;
class DivisiController extends Controller class DivisiController extends Controller
{ {
private function getAllChildren($divisi, $depth = 0, $array = []) { private function getAllChildren($divisi, $depth = 0, $array = [])
{
$divisi->depth = $depth; $divisi->depth = $depth;
array_push($array, $divisi); array_push($array, $divisi);
foreach ($divisi->children as $child) { foreach ($divisi->children as $child) {
@ -17,7 +18,8 @@ class DivisiController extends Controller
return $array; return $array;
} }
public function add(Request $request){ public function add(Request $request)
{
$this->validate($request, [ $this->validate($request, [
'name' => 'string|required|unique:m_divisi,name', 'name' => 'string|required|unique:m_divisi,name',
'description' => 'nullable|string', 'description' => 'nullable|string',
@ -36,7 +38,8 @@ class DivisiController extends Controller
return response()->json(['status' => 'success', 'message' => 'Data created!', 'code' => 200]); return response()->json(['status' => 'success', 'message' => 'Data created!', 'code' => 200]);
} }
public function update(Request $request, $id){ public function update(Request $request, $id)
{
if (empty($id) || !is_int((int)$id)) if (empty($id) || !is_int((int)$id))
return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400); return response()->json(['status' => 'failed', 'message' => 'id is required!', 'code' => 400], 400);
@ -80,13 +83,26 @@ class DivisiController extends Controller
public function search(Request $request) public function search(Request $request)
{ {
$payload = $request->all(); $parentDivisi = Divisi::whereNull('parent')->with('children')->get();
$dataBuilder = $this->setUpPayload($payload, 'm_divisi'); $divisions = [];
$builder = $dataBuilder['builder']; foreach ($parentDivisi as $objRow) {
$countBuilder = $dataBuilder['count']; $objRow->children = $this->getAllChildren($objRow);
$dataGet = $builder->get(); // $objRow->key = rand(1, 1000);
$totalRecord = $countBuilder->count(); $divisions[] = $objRow;
return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); }
$countData = count($divisions);
if ($countData == 0)
return response()->json(['status' => 'failed', 'message' => 'Data not found!', 'code' => 404], 404);
return response()->json(['status' => 'success', 'code' => 200, 'data' => $divisions, 'totalRecord' => $countData], 200);
// $payload = $request->all();
// $dataBuilder = $this->setUpPayload($payload, 'm_divisi');
// $builder = $dataBuilder['builder'];
// $countBuilder = $dataBuilder['count'];
// $dataGet = $builder->get();
// $totalRecord = $countBuilder->count();
// return response()->json(['status' => 'success', 'code' => 200, 'data' => $dataGet, 'totalRecord' => $totalRecord], 200);
//return $this->list(); //return $this->list();
// cant use builder for this case // cant use builder for this case
} }

Loading…
Cancel
Save