parent->raw($this->localKey); if ($key === null) { return null; } return $this->query()->where($this->foreignKey, $key)->first(); } /** * @param array $models */ public function load(string $name, array $models): void { $keys = $this->keys($models, $this->localKey); if ($keys === []) { return; } $found = []; foreach ($this->query()->whereIn($this->foreignKey, $keys)->get() as $related) { $found[(string) $related->raw($this->foreignKey)] ??= $related; } foreach ($models as $model) { $model->setRelation($name, $found[(string) $model->raw($this->localKey)] ?? null); } } }