本文整理汇总了PHP中Illuminate\Database\Eloquent\Model::attributesToArray方法的典型用法代码示例。如果您正苦于以下问题:PHP Model::attributesToArray方法的具体用法?PHP Model::attributesToArray怎么用?PHP Model::attributesToArray使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Illuminate\Database\Eloquent\Model
的用法示例。
在下文中一共展示了Model::attributesToArray方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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;
}
示例2: convert
/**
* Convert a value to XML.
*
* @param Model $value
* @param SimpleXMLElement $element
* @return SimpleXMLElement
* @throws CantConvertValueException
*/
public function convert($value, SimpleXMLElement $element) : SimpleXMLElement
{
if (!$value instanceof Model) {
throw new CantConvertValueException("Value is not a model.");
}
return $this->prepareElement(collect($value->attributesToArray()), $element);
}
示例3: bind
/**
* Bind Model to the form
*
* @param Model|Array $model
* @return $this
*/
public function bind($model = [])
{
/**
* Cast eloquent model to array
*/
$data = $model instanceof Model ? $model->attributesToArray() : $model;
$this->bind = $this->collection->make($data);
return $this;
}
示例4: decorate
/**
* Decorates a single Eloquent Model
*
* @param Model $item
*
* @return array
*/
public function decorate(Model $item)
{
$returnObject = [];
foreach ($item->attributesToArray() as $attribute => $value) {
if (isset($this->decorateTable[$attribute])) {
$returnObject[$this->decorateTable[$attribute]] = $value;
}
}
return $returnObject;
}
示例5: serializeEloquentModel
/**
* @param Model $value
*
* @return array
*/
protected function serializeEloquentModel(Model $value)
{
$stdClass = (object) $value->attributesToArray();
$data = $this->serializeData($stdClass);
$data[self::CLASS_IDENTIFIER_KEY] = get_class($value);
$methods = RelationshipPropertyExtractor::getRelationshipAsPropertyName($value, get_class($value), new ReflectionClass($value), $this);
if (!empty($methods)) {
$data = array_merge($data, $methods);
}
return $data;
}
示例6: attributesToArray
/**
* Convert the model's attributes to an array.
*
* @inheritdoc
*/
public function attributesToArray()
{
// unset date values that are 0, so they don't cause problems with serializeDate()
foreach ($this->getDates() as $key) {
if (!isset($this->attributes[$key])) {
continue;
}
// 0 should be considered 'unset' aswell
if (0 === $this->attributes[$key]) {
$this->attributes[$key] = null;
}
}
$attributes = parent::attributesToArray();
// make sure that our date values are strings
foreach ($this->getDates() as $key) {
if ($attributes[$key] instanceof \DateTime) {
$attributes[$key] = $attributes[$key]->format($this->getDateFormat());
}
}
return $attributes;
}
示例7: attributesToArray
/**
* Convert the model's attributes to an array.
*
* @return array
*/
public function attributesToArray()
{
$attributes = parent::attributesToArray();
// Because the original Eloquent never returns objects, we convert
// MongoDB related objects to a string representation. This kind
// of mimics the SQL behaviour so that dates are formatted
// nicely when your models are converted to JSON.
foreach ($attributes as $key => &$value) {
if ($value instanceof MongoId) {
$value = (string) $value;
}
}
// Convert dot-notation dates.
foreach ($this->getDates() as $key) {
if (str_contains($key, '.') and array_has($attributes, $key)) {
array_set($attributes, $key, (string) $this->asDateTime(array_get($attributes, $key)));
}
}
return $attributes;
}
示例8: consumeModel
/**
* Lets the model consume the old one
* @param Model $model The old model
* @return $this
*/
public function consumeModel(Model $model)
{
$this->consumedModel = $model;
$this->consumedModelProps = array_merge($model->attributesToArray(), $model->relationsToArray());
return $this;
}
示例9: attributesToArray
/**
* Convert the model's attributes to an array.
*
* @inheritdoc
*/
public function attributesToArray()
{
// unset date values that are 0, so they don't cause problems with serializeDate()
foreach ($this->getDates() as $key) {
if (!isset($this->attributes[$key])) {
continue;
}
// 0 should be considered 'unset' aswell
if (0 === $this->attributes[$key]) {
$this->attributes[$key] = null;
}
}
return parent::attributesToArray();
}
示例10: getModelData
/**
* @param Driver $serializer
* @param Model $model
*
* @return array
*/
protected static function getModelData(Driver $serializer, Model $model)
{
$stdClass = (object) $model->attributesToArray();
$data = $serializer->serialize($stdClass);
$data[Serializer::CLASS_IDENTIFIER_KEY] = get_class($model);
return $data;
}
示例11: attributesToArray
/**
* Convert the model's attributes to an array.
*
* @return array
*/
public function attributesToArray()
{
$attributes = parent::attributesToArray();
// Convert dot-notation dates.
foreach ($this->getDates() as $key) {
if (str_contains($key, '.') and array_has($attributes, $key)) {
array_set($attributes, $key, (string) $this->asDateTime(array_get($attributes, $key)));
}
}
return $attributes;
}
示例12: attributesToArray
/**
* {@inheritdoc}
*/
public function attributesToArray()
{
$attributes = parent::attributesToArray();
foreach ($attributes as $key => $value) {
if (in_array($key, $this->encrypted) && !empty($this->attributes[$key])) {
$attributes[$key] = Crypt::decrypt($value);
}
}
return $attributes;
}
示例13: getInputForModel
/**
* Return the attributes for a particular model as an array of input values
*
* @param Model $model The model for which we would like to return the input values
* @param string $prefix The prefix that needs to be added to the input
* @return array
*/
public function getInputForModel($model, $prefix = '')
{
return $this->getPrefixedInput($model->attributesToArray(), $prefix);
}
示例14: attributesToArray
/**
* @return array
*/
public function attributesToArray()
{
$attributes = parent::attributesToArray();
foreach ($this->jsonFields as $field) {
if (array_key_exists($field, $attributes)) {
$attributes[$field] = json_decode($attributes[$field], true);
}
}
foreach ($this->booleanFields as $field) {
if (array_key_exists($field, $attributes)) {
$attributes[$field] = boolval($attributes[$field]);
}
}
return $attributes;
}
示例15: attributesToArray
/**
* Convert the model's attributes to an array.
*
* @return array
*/
public function attributesToArray()
{
$attributes = parent::attributesToArray();
$attributes = $this->convertAttributes($attributes);
return $attributes;
}