diff --git a/src/Hydrator/EloquentHydrator.php b/src/Hydrator/EloquentHydrator.php index 82ae3ab9..b0c487cf 100644 --- a/src/Hydrator/EloquentHydrator.php +++ b/src/Hydrator/EloquentHydrator.php @@ -70,7 +70,7 @@ class EloquentHydrator extends AbstractHydrator implements HydratesRelatedInterf * * @var array */ - protected $attributes = []; + protected $attributes = NULL; /** * The resource attributes that are dates. @@ -115,6 +115,24 @@ public function __construct(HttpServiceInterface $service) { $this->service = $service; } + + /** + * @param $record + * @return void + */ + protected function attributeKeys($record) + { + if(is_null($this->attributes)) + { + $fillableAttributes = []; + foreach($record->getFillable() as $attribute) + { + $fillableAttributes[Str::dasherize($attribute)] = $attribute; + } + return $fillableAttributes; + } + return $this->attributes; + } /** * @inheritDoc @@ -127,7 +145,7 @@ protected function hydrateAttributes(StandardObjectInterface $attributes, $recor $data = []; - foreach ($this->attributes as $resourceKey => $modelKey) { + foreach ($this->attributeKeys($record) as $resourceKey => $modelKey) { if (is_numeric($resourceKey)) { $resourceKey = $modelKey; $modelKey = $this->keyForAttribute($modelKey, $record); diff --git a/src/Schema/EloquentSchema.php b/src/Schema/EloquentSchema.php index 01dbf6fb..7a5788ae 100644 --- a/src/Schema/EloquentSchema.php +++ b/src/Schema/EloquentSchema.php @@ -82,7 +82,7 @@ abstract class EloquentSchema extends AbstractSchema * * @var array */ - protected $attributes = []; + protected $attributes = NULL; /** * Whether resource member names are hyphenated @@ -154,6 +154,26 @@ protected function getDefaultAttributes(Model $model) return $defaults; } + /** + * Get attributes for the provided model using fillable attribute. + * + * @param Model $model + * @return array + */ + protected function attributeKeys(Model $model) + { + if(is_null($this->attributes)) + { + if(! empty($model->getVisible())) + { + return $model->getVisible(); + } + return array_diff($model->getFillable(), $model->getHidden()); + } + return $this->attributes; + } + + /** * Get attributes for the provided model. * @@ -164,7 +184,7 @@ protected function getModelAttributes(Model $model) { $attributes = []; - foreach ($this->attributes as $modelKey => $attributeKey) { + foreach ($this->attributeKeys($model) as $modelKey => $attributeKey) { if (is_numeric($modelKey)) { $modelKey = $attributeKey; $attributeKey = $this->keyForAttribute($attributeKey);