Skip to content

Commit

Permalink
Bring back relationships
Browse files Browse the repository at this point in the history
  • Loading branch information
colinhall17 committed Feb 1, 2020
1 parent 9b062f8 commit b3dc314
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions src/Traits/HasRelationships.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,59 +12,59 @@ trait HasRelationships

protected $loadedRelationships = [];

// /**
// * Get the model's relationships in array form.
// *
// * @return array
// */
// public function relationsToArray()
// {
// $attributes = [];

// foreach ($this->getArrayableRelations() as $key => $value) {
// // If the values implements the Arrayable interface we can just call this
// // toArray method on the instances which will convert both models and
// // collections to their proper array form and we'll set the values.
// if ($value instanceof Arrayable) {
// $relation = $value->toArray();
// }

// // If the value is null, we'll still go ahead and set it in this list of
// // attributes since null is used to represent empty relationships if
// // if it a has one or belongs to type relationships on the models.
// elseif (is_null($value)) {
// $relation = $value;
// }

// // If the relationships snake-casing is enabled, we will snake case this
// // key so that the relation attribute is snake cased in this returned
// // array to the developers, making this consistent with attributes.
// if (static::$snakeAttributes) {
// $key = Str::snake($key);
// }

// // If the relation value has been set, we will set it on this attributes
// // list for returning. If it was not arrayable or null, we'll not set
// // the value on the array because it is some type of invalid value.
// if (isset($relation) || is_null($value)) {
// $attributes[$key] = $relation;
// }

// unset($relation);
// }

// return $attributes;
// }

// /**
// * Get an attribute array of all arrayable relations.
// *
// * @return array
// */
// protected function getArrayableRelations()
// {
// return $this->getArrayableItems($this->relations);
// }
/**
* Get the model's relationships in array form.
*
* @return array
*/
public function relationsToArray()
{
$attributes = [];

foreach ($this->getArrayableRelations() as $key => $value) {
// If the values implements the Arrayable interface we can just call this
// toArray method on the instances which will convert both models and
// collections to their proper array form and we'll set the values.
if ($value instanceof Arrayable) {
$relation = $value->toArray();
}

// If the value is null, we'll still go ahead and set it in this list of
// attributes since null is used to represent empty relationships if
// if it a has one or belongs to type relationships on the models.
elseif (is_null($value)) {
$relation = $value;
}

// If the relationships snake-casing is enabled, we will snake case this
// key so that the relation attribute is snake cased in this returned
// array to the developers, making this consistent with attributes.
if (static::$snakeAttributes) {
$key = Str::snake($key);
}

// If the relation value has been set, we will set it on this attributes
// list for returning. If it was not arrayable or null, we'll not set
// the value on the array because it is some type of invalid value.
if (isset($relation) || is_null($value)) {
$attributes[$key] = $relation;
}

unset($relation);
}

return $attributes;
}

/**
* Get an attribute array of all arrayable relations.
*
* @return array
*/
protected function getArrayableRelations()
{
return $this->getArrayableItems(array_merge($this->oneRelationships, $this->manyRelationships));
}

/**
* Get a relationship.
Expand Down

0 comments on commit b3dc314

Please sign in to comment.