/** * Create new instance of eloquent type. * * @param Model $model */ public function __construct(Model $model, $name = '') { $this->name = $name; $this->fields = collect(); $this->hiddenFields = collect($model->getHidden())->flip(); $this->model = $model; $this->camelCase = config('relay.camel_case', false); }
/** * 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; }
/** * Applies processing to a single model * * @param Model $model * @return Model */ public function process(Model $model) { $hiddenOnModel = $model->getHidden(); foreach ($this->unhidden as $unhidden) { if (($key = array_search($unhidden, $hiddenOnModel)) !== false) { unset($hiddenOnModel[$key]); } } $hiddenOnModel = array_merge($hiddenOnModel, $this->hidden); $model->setHidden($hiddenOnModel); return $model; }
/** * Get the visible model casts for the index. * * @param \Illuminate\Database\Eloquent\Model $model * @return array */ protected function getVisibleIndexCasts(BaseModel $model) { if (count($model->getVisible()) > 0) { return array_intersect_key($model->getCasts(), array_flip($model->getVisible())); } return array_diff_key($model->getCasts(), array_flip($model->getHidden())); }
/** * ModelSnapshot constructor. * * @param Model $model * @param string $event */ protected function __construct(Model $model, $event) { $this->model = $model->makeVisible($model->getHidden()); $this->event = $event; }