When chaining withCount() and join(), the withCount() overrides the entire join. This is not the case when chaining with() and join().
$users = \App\User::withCount('followers')->join('user_settings', 'users.id', '=', 'user_settings.user_id')->get();
returns
{
id: 3,
name: "Rhea",
followers_count: 2
}...
this works however
$users = \App\User::with('followers')->join('user_settings', 'users.id', '=', 'user_settings.user_id')->get();
returns
{
id: 3,
name: "Rhea",
is_admin: 1,
verified: 1,
followers: [
{
id: 138,
user_id: 30,
is_following: 1
} ...
]
}...
I need to be able to order the resulting collection by a count of followers which is not possible via this method, which it seems it should be.
You can use something like:
User::select('*')->withCount('followers')->join(....)
This will instruct the builder to select all fields, by default only the base Model's fields are selected.
Most helpful comment
You can use something like:
This will instruct the builder to select all fields, by default only the base Model's fields are selected.