laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

PHPORM-238 Add support for `withCount` and other aggregations

Open GromNaN opened this issue 1 year ago • 0 comments

Fix PHPORM-238

Implement this feature from Laravel: https://laravel.com/docs/11.x/eloquent-relationships#counting-related-models Counting relations with SQL is done using a subquery. For MongoDB, there is 2 ways:

  1. Making a query during eager loading, that count the objects for each result ID. This is an aggregation with a $group by foreign key.
  2. Using $lookup to load references and count them. The process would be similar for embedded relations (without the $lookup). But this doesn't work with hybrid relations.

Checklist

  • [x] Add tests and ensure they pass
  • [x] Support mongodb relationships
  • [x] CANCELLED: Support hybrid relationships (PR reverted from Laravel: https://github.com/laravel/framework/pull/53679)
  • [x] Support embedded relationships, contraint query not supported PHPORM-292

GromNaN avatar Oct 17 '24 17:10 GromNaN