Preheat your oven to 375°F (190°C). Lightly grease a 9x13 inch baking dish with olive oil or cooking spray and set aside.
Bring a large pot of salted water to a rolling boil, then cook the spaghetti 2 minutes less than the package directions (it will finish in the oven). Reserve 1/4 cup pasta water before draining, then drain without rinsing.
Heat 2 tablespoons olive oil in a large skillet over medium-high heat. Add the diced onion and cook for 3 to 4 minutes until softened, then add the minced garlic and stir for 30 seconds until fragrant.
Add the ground beef or sausage to the skillet, breaking it apart with a spoon, and cook for 7 to 8 minutes until fully browned with no pink remaining. Drain off any excess fat.
Season the browned meat with Italian seasoning, garlic powder, onion powder, salt, and pepper, then pour in the marinara sauce and stir to combine. Simmer on low heat for 5 minutes, adding a splash of reserved pasta water if the sauce is too thick.
In a medium bowl, stir together the ricotta, 1 cup of the mozzarella, 1/4 cup of the Parmesan, and both eggs until fully combined and smooth. Season with a pinch of salt and pepper.
Add the drained spaghetti to the skillet with the meat sauce and toss thoroughly until every strand is well coated, adding reserved pasta water as needed if it looks dry.
Spread half the spaghetti mixture into the prepared baking dish, then drop spoonfuls of the ricotta mixture evenly over the top. Add the remaining spaghetti on top, then sprinkle with the remaining 1 cup mozzarella and 1/4 cup Parmesan.
Cover the dish tightly with aluminum foil and bake in the preheated oven for 25 minutes.
Remove the foil and bake for another 10 to 15 minutes until the cheese is melted, bubbly, and lightly golden. For extra browning, broil for 2 to 3 minutes, watching closely.
Let the casserole rest for 5 to 10 minutes before slicing and serving. Garnish with fresh parsley or basil if desired.