public function __construct() { $this->middleware('admin'); $root = Department::withTrashed()->find(Department::ROOT_DEPARTMENT); if (!$root) { $root = new Department(); $root->name = '*'; $root->acronym = '*'; $root->display_name = '*'; $root->department_id = null; $root->save(); $root->id = Department::ROOT_DEPARTMENT; $root->save(); } else { if ($root->trashed()) { $root->restore(); } if ($root->department_id != null) { $root->department_id = null; $root->save(); } } $more = Department::withTrashed()->whereDepartmentId(null)->where('id', '<>', Department::ROOT_DEPARTMENT); if ($more->count() > 0) { $more->update(['department_id' => Department::ROOT_DEPARTMENT]); } }
public static function getDepartments($id, $indent, $path) { $result = []; if ($indent < 15) { $models = Department::withTrashed()->whereDepartmentId($id)->get(); foreach ($models as $model) { $result[] = ['indent' => $indent, 'name' => $path . '.' . $model->name, 'id' => $model->id, 'parent' => $id]; $result = array_merge($result, static::getDepartments($model->id, $indent + 1, $path . '.' . $model->name)); } } return $result; }