本文整理汇总了PHP中Illuminate\Database\Eloquent\Model::setRelation方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::setRelation方法的具体用法?PHP Model::setRelation怎么用?PHP Model::setRelation使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Model
的用法示例。
在下文中一共展示了Model::setRelation方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: decorateRelations
/**
* Decorate the relationships of an Eloquent object.
*
* @param \Illuminate\Database\Eloquent\Model $atom
*
* @return \Illuminate\Database\Eloquent\Model
*/
protected function decorateRelations(Model $atom)
{
foreach ($atom->getRelations() as $relationName => $model) {
if ($model instanceof Collection) {
$model = $this->createDecorator('Collection')->decorate($model);
$atom->setRelation($relationName, $model);
} else {
$atom->setRelation($relationName, $this->decorate($model));
}
}
return $atom;
}
示例2: buildRelations
/**
* Build the view model representations of the relations.
*
* @return void
*/
protected function buildRelations()
{
$relations = $this->model->getRelations();
foreach ($relations as $relationName => $relation) {
// TODO: handle MorphToMany
//if ($relation instanceof MorphPivot) {
// dd($relation->getMorphClass(), $relation);
// dd(get_class_methods($relation));
//}
// For pivots, we'll test for the snake cased version of the class name as the
// relation's name, and check if a view model class has been defined on this
// view model's relations.
if ($relation instanceof Pivot) {
$originalRelationName = $relationName;
$relationName = $this->relationName ?: snake_case(class_basename($relation));
if (isset($this->relations[$relationName])) {
$relationViewModel = $this->relations[$relationName];
$relation = static::make($relation, $relationViewModel);
$this->model->setRelation($originalRelationName, $relation);
}
} elseif (isset($this->relations[$relationName])) {
$relationViewModel = $this->relations[$relationName];
$relation = static::make($relation, $relationViewModel);
$this->model->setRelation($relationName, $relation);
}
}
}
示例3: loadPivotAttribute
/**
* Get the pivot attribute from a model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param \Illuminate\Database\Eloquent\Relations\Relation $parentRelation
*/
public static function loadPivotAttribute(Model $model, Relation $parentRelation = null)
{
$attributes = $model->getAttributes();
foreach ($attributes as $key => $value) {
if ($key === 'pivot') {
unset($model[$key]);
$pivot = $parentRelation->newExistingPivot($value);
$model->setRelation($key, $pivot);
}
}
}
示例4: decorateModelRelations
/**
* Decorate an Eloquent models relations.
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @return void
*/
protected function decorateModelRelations(Model $model)
{
if ($relations = $model->getRelations()) {
foreach ($relations as $key => $value) {
$model->setRelation($key, $this->decorate($value));
}
}
}
示例5: setRelation
/**
* Set the specific relationship in the model.
*
* @param string $relationName
* @param mixed $value
* @param Relation|null $relation
* @return $this
*/
public function setRelation($relationName, $value, $relation = null)
{
if ($relation instanceof Relation) {
static::$relationsCache[$this->getRelationCacheKey($relationName)] = $relation;
}
return parent::setRelation($relationName, $value);
}
示例6: updateValue
/**
* Set value to relation.
*
* @param string $key
*/
public function updateValue($key)
{
$this->entity->setRelation($key, $this->getValue($key));
}