NilPortugues\Laravel5\JsonApi\Controller\JsonApiTrait::createResourceCallable PHP Method

createResourceCallable() protected method

Reads the input and creates and saves a new Eloquent Model.
protected createResourceCallable ( ) : callable
return callable
    protected function createResourceCallable()
    {
        return function (array $data, array $values, ErrorBag $errorBag) {
            $model = $this->getDataModel()->newInstance();
            foreach ($values as $attribute => $value) {
                $model->setAttribute($attribute, $value);
            }
            if (!empty($data['id'])) {
                $model->setAttribute($model->getKeyName(), $data['id']);
            }
            try {
                $model->save();
                //We need to load the model from the DB in case the user is utilizing getRequiredFields() on the transformer.
                $model = $model->fresh();
            } catch (\Exception $e) {
                $errorBag[] = new Error('creation_error', 'Resource could not be created');
                throw $e;
            }
            return $model;
        };
    }