I have a single model and I am trying to chain on multiple withCounts with different relations. Here is an example:
return MyModel::query() ->withCount(array('users'=>function($query){ $query->where('enabled', '=', 1); })) ->withCount(array('users'=>function($query){ $query->where('phone', '=', "123-4567"); })) ->get(); This doesn't work because Laravel automatically returns a snake_case field name in my results called "users_count", and basically this means the enabled section of the query is overwritten with the phone section.
I've peeked into the Builder.php class a bit and noticed that $asColumn is the name being returned: https://github.com/laravel/framework/commit/67b821dde62e64f94aa7d99f1806ecf03ea323e5, which like mentioned is just a snake_case of the relation's $name + '_count'.
Is there any way to make a custom $asColumn without using a raw query so that I can get both results? I basically want my results to be enabled_users_count and phone_users_count, or something similar. I'd also prefer not to have to do 2 different queries.