public function loadModels(Models $models, $flags = null) { $models = $models->filter(function ($model) { return $model->{$this->classKey}; }); $groups = Arr::groupBy($models->toArray(), function ($model) { return $model->{$this->classKey}; }); foreach ($groups as $modelClass => &$models) { $keys = Arr::pluckUniqueProperty($models, $this->key); if ($keys) { $models = $modelClass::whereIn($this->getForeignKey(), $keys)->setFlags($flags)->loadRaw(); } else { $models = []; } } return Arr::flatten($groups); }
/** * @param array|string $rels */ public function joinRels($rels) { $rels = Arr::toAssoc((array) $rels); $this->joinNestedRels($this->getRepo(), $rels, $this->getRepo()->getTable()); return $this; }
/** * @return array */ public function getPublicPropertyNames() { $properties = $this->getProperties(ReflectionProperty::IS_PUBLIC); return Arr::invoke($properties, 'getName'); }
/** * @dataProvider dataGroupBy */ public function testGroupBy($array, $callback, $preserveKeys, $expected) { $this->assertSame($expected, Arr::groupBy($array, $callback, $preserveKeys)); }
/** * Load all the models for the provided relations. This is the meat of the eager loading * * @param Models $models * @param array $rels * @param int $flags * @return Repo $this */ public function loadAllRelsFor(Models $models, array $rels, $flags = null) { $rels = Arr::toAssoc($rels); foreach ($rels as $relName => $childRels) { $foreign = $this->loadRelFor($models, $relName, $flags); if ($childRels) { $rel = $this->getRel($relName); $rel->getRepo()->loadAllRelsFor($foreign, $childRels, $flags); } } return $this; }