Fills a model with the necessary relationship values.

@param \Illuminate\Database\Eloquent\Model    $model
@param \Frozennode\Administrator\Fields\Field $field

@return \Illuminate\Database\Eloquent\Collection
964815
public function getModelRelatedItems($model, Field $field)
    {
        $name = $field->getOption('field_name');

        if ($field->getOption('multiple_values')) {
            //if a sort_field is provided, use it, otherwise sort by the name field
            if ($sortField = $field->getOption('sort_field')) {
                return $model->{$name}()->orderBy($sortField)->get();
            } else {
                return $model->{$name}()->get();
            }
        } else {
            return $model->{$name}()->get();
        }
    }
1095390
public function unique($model, $field) {
        $this->check_role = 'unique';
        /* @var $model Record */
        $model = new $model;
        $result = empty($model::find([$field => $this->value]));
        $this->checkResult($result);
        return $this;
    }
180855
private function populate($entity, $model)
    {
        if ($entity && (is_array($entity) || is_object($entity))) {
            foreach ($entity as $name => $value) {
                $model->$name = $value;
            }
        }

        return $model;
    }
204852
public function importFields($model, $fields = null)
    {
        $this->model = $model;
        $this->form = $this->owner;

        if ($fields === false) {
            return;
        }

        if (!$fields) {
            $fields = 'editable';
        }
        if (!is_array($fields)) {
            // note: $fields parameter is only useful if model is SQL_Model
            $fields = $model->getActualFields($fields);
        }

        // import fields one by one
        foreach ($fields as $field) {
            $this->importField($field);
        }

        // set update hook
        if (!$this->_hook_set) {
            $this->owner->addHook('update', array($this, 'update'));
            $model->addHook('afterLoad', array($this, 'setFields'));
            $this->_hook_set = true;
        }

        return $this;
    }
670530
public function createFromModel(Model $model)
    {
        $this->model = $model;
        $this->query = $this->model->newQuery();

        return $this;
    }
247739
public function getAttributes($model, array $fields = null)
    {
        $attributes = $this->attributes($model);

        if ($fields !== null) {
            $attributes = array_only($attributes, $fields);
        }

        return $attributes;
    }
1338608
public function addField(Field ...$fields)
    {
        collect($fields)->map(function ($field) {
            $this->group['fields']->put($field->key(), $field->get());
        });

        return $this;
    }
1284899
public function group($field, $id)
    {
        if (!isset($this->grouped[$field])) {
            foreach ($this->rows as $row) {
                if (!isset($this->grouped[$field][$row->$field])) {
                    $this->grouped[$field][$row->$field] = [];
                }
                $this->grouped[$field][$row->$field][] = $row;
            }
        }
        $origins = isset($this->grouped[$field][$id]) ? $this->grouped[$field][$id] : [];
        return new Rows($origins, $this->getTable(), $this, $this->model);
    }
1345434
public function save(Model $model)
    {
        if ($this->fieldId) {
            $model->setAttribute($this->fieldKey, $this->fieldId);
        }

        return parent::save($model);
    }
171822
public function getInstanceValueForFields(Model $modelInstance, $fields)
    {
        $values = [];
        /** @var $field Field */
        foreach ($fields as $field) {
            $val = $modelInstance->{$field->getAttrName()};
            if (!$val) {
                continue;
            }
            $values[] = $val;
        }

        return $values;
    }
700321
protected function handle(EloquentModel $model, $field, $value) : EloquentModel
  {
    return $model::where($field, 'like', '%' . $value . '%')->first() ?? $model;
  }
1277202
public function get($model, $field, $row)
    {
        $value = $model->getFieldValue($field);
        return $this->parse($value, $row);
    }
318581
public function pushField(Field $field)
    {
        $field = $this->formatField($field);

        $this->fields->push($field);

        return $this;
    }
480980
public function addField($fieldName, Field $field)
    {
        $this->fieldNames[ $fieldName ] = $fieldName;
        $this->fields[ $fieldName ]     = $field;

        return $fieldName;
    }
919865
public function setModel(Model $model)
    {
        if (strpos($this->originalName, '.') !== false) {
            $parts = explode('.', $this->originalName);
            $name = array_pop($parts);

            while ($parts) {
                $part = array_shift($parts);
                $relation = $model->getAttribute($part);

                if ($relation instanceof Model) {
                    $model = $relation;
                    $this->setName($name);
                }
            }
        }

        return parent::setModel($model);
    }
1201110
public function addField(Field $field)
    {
        $field
            ->setSection($this->currentSection)
            ->setOptionName($this->registry['option_name'])
        ;

        if (isset($this->registry['field_values'][$field->getId()])) {
            $field->setValue($this->registry['field_values'][$field->getId()]);
        }

        $this->registry['fields'][] = $field;

        return $this;
    }
598818
public function setModel(\atk4\data\Model $model, $fields = null)
    {
        // dont add any fields automatically
        parent::setModel($model, false);

        if ($fields === null) {
            $fields = $this->getModelFields($model);
        }

        $cnt = count($fields);

        if ($this->col !== null) {
            $col = $this->col;
            $size = $this->size;
        } elseif ($cnt < 10) {
            $col = 1;
            $size = '';
        } elseif ($cnt < 15) {
            $col = 2;
            $size = '';
        } elseif ($cnt < 20) {
            $col = 2;
            $size = 'small';
        } elseif ($cnt < 32) {
            $col = 3;
            $size = 'small';
        } else {
            $col = 4;
            $size = 'tiny';
        }

        if ($size) {
            $this->form->addClass($size);
        }

        $c = $this->add('Columns');

        $chunks = array_chunk($fields, ceil($cnt / $col));
        foreach ($chunks as $chunk) {
            $cc = $c->addColumn();
            $cc->add(['FormLayout/Generic', 'form' => $this->form])->setModel($model, $chunk);
        }

        $this->add(['ui' => 'clearing hidden divider']);

        return $model;
    }