I have the following code to get some specific events (Events of Clubs that a user has liked)
$events = Event::query();
$events->with('club');
$events->with('hasUserTicket');
$events->with('hasUserGuestlist');
$events->withCount('tickets');
$events->withCount('guestlist');
$events->join('clubs', 'clubs.id', '=', 'events.club_id');
$events->join('likeables', 'likeables.likeable_id', '=', 'clubs.id');
$events->join('users', 'users.id', '=', 'likeables.user_id');
$events->select('clubs.*', 'clubs.name as clubs.name', 'events.*');
$events->where('likeables.likeable_type', 'App\\Club');
$events->where('users.id', $user->id);
$events->orderBy('events.date', 'ASC');
return $events->get();
My problem is, that because of the line _$events->select.._. the two columns tickets_count & guestlist_count (from _->withCount('guestlist');_ & _->withCount('tickets');_) are missing in the result.
Is that expected behaviour? Because the values of hasUserTicket and hasUserGuestlist are available in the result. Or do I need to explicitly call the two properties in the select method and if so, how?
Okay, so I figured out that it works when I put withCount after the select method
->select('clubs.*', 'clubs.name as clubs.name', 'events.*')
->withCount('tickets')
->withCount('guestlist')
Strange (to me) but good that it works!
When encapsulating the withCount in a function within a EloquentModel we need to use the select after the encapsulated function as the select return QueryBuilder so not sure how to solve that yet
Most helpful comment
Okay, so I figured out that it works when I put withCount after the select method
Strange (to me) but good that it works!