本文整理汇总了PHP中Illuminate\Database\Eloquent\Model::getRelations方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::getRelations方法的具体用法?PHP Model::getRelations怎么用?PHP Model::getRelations使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Model
的用法示例。
在下文中一共展示了Model::getRelations方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: saveRelations
private function saveRelations()
{
/** @var Eloquent $related */
foreach ($this->original->getRelations() as $related) {
if ($related->isDirty()) {
if ($related->save() === false) {
return false;
}
}
}
return true;
}
示例2: toArray
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
$model = [];
foreach ($this->model->attributesToArray() as $attribute => $value) {
$model[$attribute] = $this->{$attribute};
if (is_object($model[$attribute]) && is_callable([$model[$attribute], 'toArray'])) {
$model[$attribute] = $model[$attribute]->toArray();
}
}
foreach ($this->model->getRelations() as $relationName => $relation) {
try {
$pivotRelationName = $relation->relationName;
} catch (Exception $e) {
// Fall though...
}
if (isset($pivotRelationName)) {
$relationName = $pivotRelationName;
}
if (is_object($relation) && is_callable([$relation, 'toArray'])) {
$model[$relationName] = $relation->toArray();
} else {
$model[$relationName] = $relation;
}
}
if (!empty($this->additionalAttributes)) {
foreach ($this->additionalAttributes as $attribute) {
$model[$attribute] = $this->{$attribute};
if (is_object($model[$attribute]) && is_callable([$model[$attribute], 'toArray'])) {
$model[$attribute] = $model[$attribute]->toArray();
}
}
}
return $model;
}
示例3: castModel
/**
* Get an array representing the properties of a model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return array
*/
public static function castModel(Model $model)
{
$attributes = array_merge($model->getAttributes(), $model->getRelations());
$visible = array_flip($model->getVisible() ?: array_diff(array_keys($attributes), $model->getHidden()));
$results = [];
foreach (array_intersect_key($attributes, $visible) as $key => $value) {
$results[(isset($visible[$key]) ? Caster::PREFIX_VIRTUAL : Caster::PREFIX_PROTECTED) . $key] = $value;
}
return $results;
}
示例4: 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;
}
示例5: 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));
}
}
}
示例6: linksToEntityRelations
/**
* Creates Links to all of the provided Model's relations
*
* Keep in mind that relations may only be scanned after they are attached to a certain Model, which means "join"
* operation(s) where triggered. For example: with() method was called on the Model with params!
*
* @param Model $ent The Model that provides the Relations for our links
* @param bool $forceReturn If set to true, it will return an empty array, and won't throw an Exception
* @throws \InvalidArgumentException
* @return array
*/
public function linksToEntityRelations(Model $ent, $forceReturn = false)
{
$links = [];
$relations = $ent->getRelations();
if (!is_array($relations) || count($relations) == 0) {
if ($forceReturn === false) {
throw new \InvalidArgumentException('Provided Entity does not contain any relations, can\'t generate links...');
}
return $links;
}
foreach ($relations as $key => $rel) {
if ($rel instanceof ResourceEntity) {
$links[] = $this->createLink($rel->getRootRelName(), $this->request->url() . '/' . $rel->getRootRelName());
} else {
$links[] = $this->createLink($key, $this->request->url() . '/' . $key);
}
}
return $links;
}
示例7: resolveRelations
/**
* Resolve eager loaded relations from the model.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return array
*/
protected function resolveRelations(Model $model) : array
{
return array_keys($model->getRelations());
}