Open ronrun opened 1 year ago
Describe the bug According to the docs, this is eager loading. Post::withTranslation()->get(); It actually is. But when do the foreach
Post::withTranslation()->get();
foreach ($rows as $key => $row) { echo '<pre>', print_r($row->translation->name, 1), "</pre>"; }
It does the sql query for every row. If 10 rows, it's 1 sql for my product table, 10 sql for product_relations table
But if I use laravel's default's with() Product::with('translation')->get(); It's really eager loading.
Product::with('translation')->get();
So, maybe there is something wrong with the function withTranslation() ?
To Reproduce
//$products = (new Product)->with('translation')->get(); $products = (new Product)->withTranslation()->get(); // echo '<pre>', print_r($products, 1), "</pre>"; // This is always eager loading. foreach ($products as $key => $row) { echo '<pre>', print_r($row->translation->name, 1), "</pre>"; // This depends. }
It doesn't load the translation relationship but filtered translations.
translation
translations
https://github.com/Astrotomic/laravel-translatable/blob/a030356edc6a97a834b9e603d0997f0eb684cfdb/src/Translatable/Traits/Scopes.php#L109-L124
Describe the bug According to the docs, this is eager loading.
Post::withTranslation()->get();
It actually is. But when do the foreachIt does the sql query for every row. If 10 rows, it's 1 sql for my product table, 10 sql for product_relations table
But if I use laravel's default's with()
Product::with('translation')->get();
It's really eager loading.So, maybe there is something wrong with the function withTranslation() ?
To Reproduce