public function postAdd() { $v = Validator::make(Request::all(), array('name' => 'required', 'copy' => 'required|integer')); if ($v->passes()) { $role = new UserRole(); $role->name = Request::input('name'); $role->admin = 1; $role->save(); $role_info = array(); $copy = UserRole::find(Request::input('copy')); if (!empty($copy)) { $copy_actions = array(); $controller = AdminController::where('controller', '=', 'roles')->first(); foreach ($copy->actions as $action) { // don't copy role permissions if ($controller->id != $action->controller_id) { array_push($copy_actions, $action->id); } } $role->actions()->sync($copy_actions); foreach ($copy->page_actions as $page_action) { $role->page_actions()->attach($page_action->id, ['action_id' => $page_action->pivot->action_id, 'access' => $page_action->pivot->access]); } } $role_info[$role->id] = $role->name; return json_encode($role_info); } return 0; }