Cast an clean the incoming attributes so that this repository can
savely pass them to the database.
The attributes have to be validated before passing them to this method.

@param mixed $model
@param array $attributes

@return array
327589
protected function toModelAttributes($model, $attributes)
    {
        $filtered = [];
        $filter = $this->getAttributeFilter();

        foreach ($attributes as $key => $value) {
            if (!$filter($key, $value)) {
                continue;
            }

            $filtered[$key] = $value;
        }

        return $filtered;
    }
425401
protected function getFactoryAttributes(Model $model)
    {
        // This package allows developers to specify attributes for single locales
        // in their factory definitions with the attribute:locale syntax,
        // but the Eloquent\FactoryBuilder's raw() method interprets them as regular attributes,
        // and if we were to make() the model and then call getAttribute() everytime,
        // such attributes would cause Translatable to run a query for each model instance.
        // We'll resort to binding a closure to the factory to get the defined states,
        // and pass them as custom attributes to raw().
        // This has the added bonus over make()->getAttributes() of delaying the calling
        // of any closure in factory definitions/states, allowing them to receive the model
        // with more attributes set and the inserted primary keys as arguments,
        // like the closure custom attributes passed to Populator.
        $factory = app(Eloquent\Factory::class);

        $states = $this->isTranslation($model) ? $this->translationStates : $this->states;

        $modelClass = get_class($model);

        $modelIsDefined = call_user_func(\Closure::bind(function () use ($modelClass) {
            return isset($this->definitions[$modelClass]);
        }, $factory, $factory));

        if (!$modelIsDefined) {
            // If the model is not defined in the factory and the developer didn't apply any state,
            // that means he doesn't need the factory for this model, so we'll return an empty array.
            if (!$states) {
                return [];
            }

            // If the model doesn't have a factory definition,
            // but the developer still wants to apply states,
            // we'll create a dummy definition of the model to allow it.
            $factory->define($modelClass, function () {
                return [];
            });
        }

        return $factory->raw($modelClass, $this->getStateAttributes($factory, $states, $modelClass));
    }
210382
protected function recordableAttributes($model, $attributes)
    {
        $except = array_flip($this->config('ignore', $model));

        return array_diff_key($attributes, $except);
    }
581801
protected function prepareAttributes($attributes)
    {
        foreach($this->getAttributeConfig() as $attr => $config){

            // If the attribute is not defined in model or $attributes array
            // set the default value.
            if(!isset($this->attributes[$attr]) && !isset($attributes[$attr])){

                $attributes[$attr] = $config['default'];

            }

        }

        foreach($attributes as $key => $value){

            if(isset($this->config['attributes'][$key])){

                $attributes[$key] = $this->$key()->before_save($value);

            } elseif(isset($this->config['relationships'][$key])){

                unset($attributes[$key]);

                $relationship = $this->config['relationships'][$key];

                $this->translateShorthand($value);

                switch($relationship['type']){

                    case Relationship::HAS_PIVOT:

                        if($value instanceof Collection) $value = $value->all();

                        if($value instanceof EntityContract) $value = [$value];

                        $this->$key()->saveMany($value);

                        break;

                    case Relationship::HAS_MANY:

                        $this->$key()->saveMany((array)$value);

                        break;

                    case Relationship::BELONGS_TO:

                        $this->$key()->associate($value);

                        break;

                    case Relationship::HAS_ONE:

                        $this->$key()->save($value);

                        break;

                    default:

                        throw new \Exception(sprintf(
                            "Unknown relationship type \"%s\" on entity \"%s\"",
                            $relationship['type'],
                            $relationship['entity']
                        ));

                        break;
                }

            }

        }

        return $attributes;
    }
229558
protected static function clearAttributes(array $attributes, array $clear): array
    {
        foreach ($clear as $attributeName) {
            if (array_key_exists($attributeName, $attributes)) {
                unset($attributes[$attributeName]);
            }
        }
        return $attributes;
    }
25196
protected function getSoftDeleteMethods($model)
    {
        $traits = class_uses(get_class($model), true);
        if (in_array('Illuminate\\Database\\Eloquent\\SoftDeletes', $traits)) {
            $this->setMethod('forceDelete', 'bool|null', []);
            $this->setMethod('restore', 'bool|null', []);

            $this->setMethod('withTrashed', '\Illuminate\Database\Query\Builder|\\' . get_class($model), []);
            $this->setMethod('withoutTrashed', '\Illuminate\Database\Query\Builder|\\' . get_class($model), []);
            $this->setMethod('onlyTrashed', '\Illuminate\Database\Query\Builder|\\' . get_class($model), []);
        }
    }
1276735
protected function findRecord($model, $attributes = [])
    {
        $this->getModelRecord($model);
        $conditions = [];
        $bind       = [];
        foreach ($attributes as $key => $value) {
            if ($value === null) {
                $conditions[] = "$key IS NULL";
            } else {
                $conditions[] = "$key = :$key:";
                $bind[$key] = $value;
            }
        }
        $query = implode(' AND ', $conditions);
        $this->debugSection('Query', $query);
        return call_user_func_array([$model, 'findFirst'], [
            [
                'conditions' => $query,
                'bind'       => $bind,
            ]
        ]);
    }
665733
protected function castAttributes($attributes)
    {
        return $this->using
                    ? $this->newPivot()->fill($attributes)->getAttributes()
                    : $attributes;
    }
1102857
protected function getDeletedEventAttributes(): array
    {
        $old = [];

        foreach ($this->attributes as $attribute => $value) {
            if ($this->isAttributeAuditable($attribute)) {
                $old[$attribute] = $value;
            }
        }

        return [
            $old,
            [],
        ];
    }
421588
public function resolveModelAttribute($model, $attribute = null)
    {
        if ($attribute === null) {
            $attribute = $this->valueFrom ?: $this->fieldName;
        }

        $parts = is_array($attribute) ? $attribute : HtmlHelper::nameToArray($attribute);
        $last = array_pop($parts);

        foreach ($parts as $part) {
            $model = $model->{$part};
        }

        return [$model, $last];
    }
737862
public function attributesToArray()
    {
        $attributes = $this->attributes;

        // If an attribute is a date, we will cast it to a string after converting it
        // to a DateTime / Carbon instance. This is so we will get some consistent
        // formatting while accessing attributes vs. arraying / JSONing a object.
        foreach ($this->getDates() as $key) {
            if (! isset($attributes[$key])) {
                continue;
            }

            $attributes[$key] = $this->serializeDate(
                $this->asDateTime($attributes[$key])
            );
        }

        $mutatedAttributes = $this->getMutatedAttributes();

        // We want to spin through all the mutated attributes for this object and call
        // the mutator for the attribute. We cache off every mutated attributes so
        // we don't have to constantly check on attributes that actually change.
        foreach ($mutatedAttributes as $key) {
            if (! array_key_exists($key, $attributes)) {
                continue;
            }

            $attributes[$key] = $this->mutateAttributeForArray(
                $key, $attributes[$key]
            );
        }

        // Next we will handle any casts that have been setup for this object and cast
        // the values to their appropriate type. If the attribute has a mutator we
        // will not perform the cast on those attributes to avoid any confusion.
        foreach ($this->getCasts() as $key => $value) {
            if (! array_key_exists($key, $attributes) ||
                in_array($key, $mutatedAttributes)) {
                continue;
            }

            $attributes[$key] = $this->castAttribute(
                $key, $attributes[$key]
            );

            if ($attributes[$key] && ($value === 'date' || $value === 'datetime')) {
                $attributes[$key] = $this->serializeDate($attributes[$key]);
            }
        }

        return $attributes;
    }
402767
public function create($attributes)
    {
        $attributes = $this->castRequest($attributes);
        $instance   = $this->model->newInstance($attributes);

        event(implode('-', $this->tag).'.entity.creating', [$this, $instance, $attributes]);

        $created = $instance->save();

        event(implode('-', $this->tag).'.entity.created', [$this, $instance, $attributes]);

        return [
            $created,
            $instance,
        ];
    }
1077962
public function fill(Identifiable $model, array $attributes)
    {

        $model = $this->checkTypeAndReturn($model);
        $changed = false;

        foreach ($attributes as $key=>$value) {
            // Check for exact comparison
            if(isset($model[$key]) && $model[$key] === $value) {
                continue;
            }
            $model[$key] = $value;
            $changed = true;
        }
        return $changed;
    }
594589
public function attributesToArray()
    {
        $attributes = $this->getArrayableAttributes();

        // Process attributes
        foreach ($attributes as $key => $value) {
            // Convert objects to array
            if ($this->hasToArray($value)) {
                $attributes[$key] = $value->toArray();
            }

            // If an attribute is a date, we will cast it to a string after converting it
            // to a DateTime / \Jenssegers\Date\Date instance. This is so we will get some consistent
            // formatting while accessing attributes vs. arraying / JSONing a model.
            if (in_array($key, $this->getDates())) {
                $attributes[$key] = $this->serializeDate(
                    $this->asDateTime($value)
                );
            }
        }

        $mutatedAttributes = $this->getMutatedAttributes();

        // We want to spin through all the mutated attributes for this model and call
        // the mutator for the attribute. We cache off every mutated attributes so
        // we don't have to constantly check on attributes that actually change.
        foreach ($mutatedAttributes as $key) {
            if (!array_key_exists($key, $attributes)) {
                continue;
            }

            $attributes[$key] = $this->mutateAttributeForArray(
                $key, $attributes[$key]
            );
        }

        // Next we will handle any casts that have been setup for this model and cast
        // the values to their appropriate type. If the attribute has a mutator we
        // will not perform the cast on those attributes to avoid any confusion.
        foreach ($this->casts as $key => $value) {
            if (!array_key_exists($key, $attributes) ||
                in_array($key, $mutatedAttributes)
            ) {
                continue;
            }

            $attributes[$key] = $this->castAttribute(
                $key, $attributes[$key]
            );

            if ($attributes[$key] && ($value === 'date' || $value === 'datetime')) {
                $attributes[$key] = $this->serializeDate($attributes[$key]);
            }
        }

        // Here we will grab all of the appended, calculated attributes to this model
        // as these attributes are not really in the attributes array, but are run
        // when we need to array or JSON the model for convenience to the coder.
        foreach ($this->getArrayableAppends() as $key) {
            $attributes[$key] = $this->mutateAttributeForArray($key, null);
        }

        return $attributes;
    }
1028284
public function fill(Identifiable $model, array $attributes)
    {
        $this->checkIsModel($model);

        $this->callBeforeListeners('fill', [$model, $attributes]);
        $filtered = $this->toModelAttributes($model, $attributes);

        /** @var EloquentModel $model */
        $beforeDirty = $model->isDirty();
        $this->performFill($model, $filtered);
        $changed = $beforeDirty || $model->isDirty();

        $this->callAfterListeners('fill', [$model, $attributes]);

        return $changed;
    }
597109
protected function getBatchesForAttributes(array $attributes)
    {
        $batches = [];

        $lcAttributes = array_map('strtolower', $attributes);
        foreach ($this->batches as $index => $batch) {
            /** @var Batch $batch */
            if (in_array(strtolower($batch->getAttribute()), $lcAttributes)) {
                $batches[$index] = $batch;
            }
        }

        return $batches;
    }
494891
private function translateAttributes($attributes)
    {
        $translatedAttributes = [];

        foreach ($attributes as $key => $setting) {
            if (array_key_exists($key, $this->attributesTranslate)) {
                $key = $this->attributesTranslate[ $key ];
            }

            $translatedAttributes[ $key ] = $setting;
        }

        return $translatedAttributes;
    }