public function store() { $validator = Validator::make(Request::all(), ['name' => 'required|max:255', 'cname' => 'required|max:10', 'perm' => 'required']); if ($validator->fails()) { return back()->withErrors($validator)->withInput(); } $id = (int) Request::input('id'); $pid = (int) Request::input('pid'); $name = Request::input('name'); $cname = Request::input('cname'); $desc = Request::input('desc'); $perm = Request::input('perm'); if ($id) { $role = Term::find($id); //检测类别 不能加到 不同的分类和子分类 $terms = Term::get_item_by_type('role'); $terms = array_assort($terms, 'id', 'pid', 0, $id); if ($pid && $pid != $role->pid && !in_array($pid, array_keys($terms))) { $validator->errors()->add('type', '类别不正确'); return back()->withErrors($validator)->withInput(); } $role->id = $id; } else { $role = new Term(); $role->type = 'role'; } $role->pid = $pid; $role->name = $name; $role->cname = $cname; $role->desc = $desc; $role->save(); $role->set_value_by_key('perm', $perm); return redirect()->action('Admin\\RoleController@index'); }
/** * 获取指定类别 * @param $type * @param int $top_id * @param int $bottom_id * @return array */ public static function get_item_by_type($type, $top_id = 0, $bottom_id = -1) { if ($type) { $terms = self::where('type', $type)->orderBy('id', 'desc')->get()->toArray(); } else { $terms = self::orderBy('id', 'desc')->get()->toArray(); } $terms = array_assort($terms, 'id', 'pid', $top_id, $bottom_id); return $terms; }