Preheat the oven to 350°F (180°C) and lightly grease a large casserole or baking dish.
In a large skillet over medium-high heat, melt 2 tablespoons of butter.
2 tablespoons butter
Add the onion, garlic, salt, and pepper. Cook for 5–6 minutes, stirring often, until the onions are soft and fragrant. Remove from heat and set aside.
1 large onion, 2 cloves garlic, Salt and freshly ground black pepper
In a medium saucepan, melt 4 tablespoons of butter over medium heat. Whisk in the flour and cook for 1 minute, stirring constantly to form a smooth roux.
4 tablespoons butter, 6 tablespoons all-purpose flour
Gradually whisk in the milk, a little at a time, to prevent lumps. Once smooth, stir in the heavy cream and ¾ of the cheddar cheese. Continue whisking until the cheese is melted and the sauce is thick and creamy. Remove from heat.
2 ⅓ cups whole milk, ¾ cup heavy cream, 1 ½ cups mature cheddar cheese
Layer half of the potato slices in the prepared baking dish. Top with half of the sautéed onions, cheese sauce, and ham. Season lightly with salt and pepper. Repeat the layers with the remaining potatoes, onions, ham, seasoning, and cheese sauce. Sprinkle the top with the remaining cheddar and all of the Gruyère cheese.
1 ½–2 cups leftover cooked ham, 4 medium potatoes, 1 cup Gruyère cheese
Cover the dish tightly with foil and bake for 75–90 minutes, or until the potatoes are tender when pierced with a fork. Remove the foil and bake uncovered for an additional 15–20 minutes, until the top is golden brown and bubbling.
Let the dish rest for 5 minutes before serving. Garnish with fresh parsley if desired.
1 tablespoon fresh parsley