I'm accessing the user's name in the frontend (Inertia + Vue), so I assume it's trying to resolve cartItems.user more than once.
Queries are executed on the back end. To cause a database query, you'd have to make some kind of API call that triggers one. Reading props on the front end does nothing on its own.
Relations are loaded in separate database queries. When you do OrderGroup::query()->..., only the order groups are loaded in the first query, then a secondary query is executed for each relation, using the foreign keys retrieved in the first query. You may see the same query executed on users multiple times if you have other queries in the controller (or middlewares).
The duplicate queries probably come somewhere from those relations. I don't the database structure so I can't say where.
If you have Telescope enabled in development, you can pinpoint where the queries come from by checking the Queries page.
Have you tried temporarily removing individual relations from the ->where() array to see if the duplicates go away? That's the fastest way to see if they're the cause. The other is to check the relations themselves and think about how Laravel has to retrieve them.
Ps. Debugbar always shows the latest request. If you make any API calls from the order group page, Debugbar will display whatever queries were executed during those. You can check the request history tab (on the right side of the bar) to see which request exactly causes those duplicates.
Install Laravel Telescope to see which queries are duplicated. Often, some other part of your view or logic outside this controller is causing the N+1.
Just figured out that the duplicate issue was happening because the ordergroup has a host ID (user ID) that matched the one in the cart. That’s not always the case, so it’s actually normal behavior.
Also, just wanted to shout out Telescope, definitely a game changer for me. 🙌
Oh, and learned something new today: automaticallyEagerLoadRelationships. Definitely gonna keep that in my back pocket!
Appreciate all the help, as always! Thanks, guys! 😊