public function testManyEagerLoadsAndConstraints()
{
$manufacturers = Manufacturer::with(['Robots' => function ($builder) {
$builder->where('id < 25');
}, 'Robots.Bugs' => function ($builder) {
$builder->limit(2);
}, 'Robots.Parts'], ['id < 50']);
$this->assertEquals(array_sum(array_map(function ($o) {
return count($o->robots);
}, $manufacturers)), Robot::count(['id < 25 AND manufacturer_id < 50']));
$this->assertEquals(array_sum(array_map(function ($o) {
$c = 0;
foreach ($o->robots as $r) {
$c += count($r->bugs);
}
return $c;
}, $manufacturers)), 2);
$manufacturers = Manufacturer::with(['Robots.Bugs' => function ($builder) {
$builder->where('id > 10000');
}], ['limit' => 5, 'order' => 'id ASC']);
$this->assertEquals(array_sum(array_map(function ($o) {
return count($o->robots);
}, $manufacturers)), Robot::count(['manufacturer_id < 6']));
$robots = [];
foreach ($manufacturers as $m) {
$robots = array_merge($robots, $m->robots);
}
$this->assertEquals(array_sum(array_map(function ($o) {
return count($o->bugs);
}, $robots)), 0);
}