Hi laravel community, i have a problem understanding the query i do to fetch some hasMany relations count.
Actually to fetch relations count on my user i do the following query :
$user = Auth::user()->withCount('announces', 'events')->with('badges', 'votes', 'image')->where('id', Auth::id())->first();
First of all this query work just fine, but i get a verry new model instance from it wich isn't what i want.
There is 2 problem with this query, first i pass an existing model instance wich only serve as "wich Eloquent model to retrieve", so i need to specify the where to retrieve the actual Authenticated user, otherwise every user is fetched from the database wich is a waste of perf, actually, replacing the Auth::user() by User::find(Auth::id()) would do the exact same thing.
The second problem is that the User class instance informations is totally refreshed wich is also a little waste of performance, if i set a random ID before running the query it get updated, i would like to return the Auth::user() with the new query informations instead of refreshing it totally, wich is kinda related to the first problem.
My question is, it is possible to use an existing model instance to help a query and updating only the withCount()->with() of this instance ? Why the query builder ins't able to understand that im actually pointing him the exact model to retrieve ?
I hope it make sense with my average english skill.
This is really not big of a deal but i found the Auth::user() useless in this case since it is not used at all for my query, wich is sad.