-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Eager loading does not work by nested relationships using withCount and loadCount #52782
Comments
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
I have an even weirder behavior. I have translation tables in the original code for multi language site. When I fix the upper counting problem with public function productsCount(){
return $this->products()
->selectRaw('category_id, COUNT(id) as count')
->groupBy('category_id');
} and $factories = Factory::all();
$factories->load('translation');
$factories->load('categories');
$factories->load('categories.translation');
$factories->load('categories.productsCount'); and {{$category->productsCount->value('count')}} Then the |
Can somebody confirm that the first code reproduces the bug? If not, then I'll check, maybe it needs more queries and tables, but it is there. |
Turned out this is not a bug. The view needs |
Laravel Version
11.21.0
PHP Version
8.2.0
Database Driver & Version
MySQL 8.0.31
Description
I have factories, categories and products in my database. Each factory has multiple categories and each category has multiple products. These are 1:n relationships. So for example I use
return $this->hasMany(Product::class);
in theCategory
class. In my query I load all categories for all factories and I want to eager load the product count for all categories, but not the products. I do it this way:Normally this can be done with a single SQL query, but according to the logs it takes one SQL query for each category instead...
In my blade template I get the product count this way:
This appears to be a bug for me, at least I tried it with both with and load with the same result.
Steps To Reproduce
migration:
models
controller
view:
AppServiceProvider
If you check the log, then there will be multiple queries in it something like
[2024-09-13 14:26:52] local.INFO: select count(*) as aggregate from
products
whereproducts
.category_id
= ? andproducts
.category_id
is not null {"bindings":[13],"time":0.69}The text was updated successfully, but these errors were encountered: