/** * Add relation based constraints. * * @param Illuminate\Database\Eloquent\Relations\Relation $relation * @param array $args */ public function addDefinedConstraintsToRelation($relation, $args = null) { if ($args === null) { $args = $this->parent->getRelationDefinition($this->relationName); } /* * Pivot data (belongsToMany, morphToMany, morphByMany) */ if ($pivotData = array_get($args, 'pivot')) { $relation->withPivot($pivotData); } /* * Pivot timestamps (belongsToMany, morphToMany, morphByMany) */ if (array_get($args, 'timestamps')) { $relation->withTimestamps(); } /* * Count "helper" relation */ if ($count = array_get($args, 'count')) { if ($relation instanceof BelongsToManyBase) { $relation->countMode = true; } $countSql = $this->parent->getConnection()->raw('count(*) as count'); $relation->select($relation->getForeignKey(), $countSql)->groupBy($relation->getForeignKey()); } }
/** * Duplicate a one-to-many style relation where the foreign model is ALSO * cloned and then associated * * @param Illuminate\Database\Eloquent\Relations\Relation $relation * @param string $relation_name * @param Illuminate\Database\Eloquent\Model $clone * @return void */ protected function duplicateDirectRelation($relation, $relation_name, $clone) { $relation->get()->each(function ($foreign) use($clone, $relation_name) { $this->duplicate($foreign, $clone->{$relation_name}()); }); }
function getClassNameForMorphType($type) { $morphMap = Illuminate\Database\Eloquent\Relations\Relation::morphMap(); return $morphMap[$type] ?? null; }
/** * Return the relation withTrashed if being restored. * * @param Illuminate\Database\Eloquent\Relations\Relation $relation * * @return Illuminate\Database\Eloquent\Relations\Relatio */ private function nestedRelation($relation) { if ($this->direction == 'restore') { return $relation->withTrashed(); } return $relation; }