-
-
Notifications
You must be signed in to change notification settings - Fork 170
Open
Description
Describe the bug
According to the docs, this is eager loading.
Post::withTranslation()->get();
It actually is. But when do the foreach
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.
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.
}