public function run() { DB::table('regions')->delete(); $region = new Region(); $region->code = 'ARG'; $region->name = 'Argentina'; $region->description = 'Nacional'; $region->color = '#37ABC8'; $region->icon = 'argentina'; $region->save(); $region = new Region(); $region->code = 'CBA'; $region->name = 'Córdoba'; $region->description = 'Córdoba'; $region->color = '#EE441E'; $region->icon = 'cordoba'; $region->parent_id = 1; $region->save(); $region = new Region(); $region->code = 'STA'; $region->name = 'Santa Fé'; $region->description = 'Santa Fé'; $region->color = '#8FC521'; $region->icon = 'santa_fe'; $region->parent_id = 1; $region->save(); $region = new Region(); $region->code = 'MZA'; $region->name = 'Mendoza'; $region->description = 'Mendoza'; $region->color = '#F3558A'; $region->icon = 'mendoza'; $region->parent_id = 1; $region->save(); $userRegion = new UserRegion(); $userRegion->user_id = 1; $userRegion->region_id = 1; $userRegion->admin = true; $userRegion->save(); }
public function assignRegions(Request $request) { $user = User::find($request['user']['sub']); if ($user->hasRole('crud_user')) { DB::transaction(function () use($request) { $user_id = $request->input('userId'); $regions = $request->input('regions'); DB::table('users_regions')->where('user_id', '=', $user_id)->delete(); foreach ($regions as $region) { $userRole = UserRegion::firstOrCreate(array('user_id' => $user_id, 'region_id' => $region['id'], 'admin' => true)); } }); } else { return response()->json(['message' => 'No tienes permisos para esta operacion'], 401); } return response()->json(['message' => 'Regiones asignadas'], 200); }