public function byDepartment(Request $request) { $department = new Department(); $d1 = array_flatten($department->whereNotIn('code', ['KIT'])->orderBy('code', 'DESC')->get(['id'])->toArray()); $depts = [['name' => 'Dining', 'employees' => [], 'deptid' => $d1], ['name' => 'Kitchen', 'employees' => [], 'deptid' => ['71B0A2D2674011E596ECDA40B3C0AA12']]]; for ($i = 0; $i <= 1; $i++) { $employees = Employee::with('position')->select('lastname', 'firstname', 'positionid', 'employee.id')->join('position', 'position.id', '=', 'employee.positionid')->where('branchid', $request->user()->branchid)->whereIn('deptid', $depts[$i]['deptid'])->orderBy('employee.lastname', 'ASC')->orderBy('employee.firstname', 'ASC')->get(); $depts[$i]['employees'] = $employees; } return $depts; }