protected function makeAndBind($var, $key = null) { $source = DataSource::make($var); if ($var instanceof Collection && func_num_args() > 1) { $relation = Cache::getRelation($this->getDelegatedStorage(), $key); if (!$relation instanceof Relation) { throw new \LogicException('Expected Relation, got ' . get_type_class($relation)); } $source->bindToRelation($relation); } return $source; }
public function cacheAndAssociate($key, $modelOrCollection, $relation = null) { if (func_num_args() < 3) { $relation = Cache::getRelation($this->getDelegatedStorage(), $key) ?: $this->getValueOrRelationForKey($key); if (!$relation instanceof Relation) { throw new \LogicException('Relation expected, got ' . get_type_class($relation)); } $this->supportedRelationOrThrow($key, $relation); } // we set the relation for later use (saving) Cache::set($this->getDelegatedStorage(), $key, $relation, $modelOrCollection); // and we associate the child model with it if ($modelOrCollection instanceof Model) { $modelOrCollection = [$modelOrCollection]; } foreach ($modelOrCollection as $model) { Rel::make($relation)->associate($key, $model); } }