public function create($attributes = [])
    {
        // Attributes have to be array
        if (!(isset($attributes)) || !is_array($attributes)) {
            throw new \InvalidArgumentException('Undefined attributes', 400);
        }

        // Perform before hook
        $attributes = $this->beforeCreate($attributes);

        // Remove non fillable attributes
        $fields = array_intersect_key($attributes, array_flip($this->fillable));

        // Validate attributes
        $this->setValidationRules();
        $errors = $this->validationErrors($fields);
        if ($errors) {
            throw new Exceptions\ValidationException('Invalid attributes', 400, $errors);
        }

        // Create the model and return its ID
        $result = $this->db()->table($this->table)->insertGetId($fields);

        // Return false if create fails
        if (!$result) {
            throw new \Exception('Could not create the model', 500);
        }

        // Sync many to many relationships
        $this->sync($result, $attributes);

        // Upsert has many relationships
        $this->updateHasMany($result, $attributes);

        // Get the created model
        $model = $this->find($result);

        // Run after hook
        $model = $this->afterCreate($result, $attributes, $model);

        // Return the created model
        return $model;
    }
public function create($attributes = [])
    {
        // Attributes have to be array
        if (!(isset($attributes)) || !is_array($attributes)) {
            throw new \InvalidArgumentException('Undefined attributes', 400);
        }

        // Perform before hook
        $attributes = $this->beforeCreate($attributes);

        // Remove non fillable attributes
        $fields = array_intersect_key($attributes, array_flip($this->fillable));

        // Validate attributes
        $this->setValidationRules();
        $errors = $this->validationErrors($fields);
        if ($errors) {
            throw new Exceptions\ValidationException('Invalid attributes', 400, $errors);
        }

        // Create the model and return its ID
        $result = $this->db()->table($this->table)->insertGetId($fields);

        // Return false if create fails
        if (!$result) {
            throw new \Exception('Could not create the model', 500);
        }

        // Sync many to many relationships
        $this->sync($result, $attributes);

        // Upsert has many relationships
        $this->updateHasMany($result, $attributes);

        // Get the created model
        $model = $this->find($result);

        // Run after hook
        $model = $this->afterCreate($result, $attributes, $model);

        // Return the created model
        return $model;
    }
public function create ( $ attributes = [ ] ) { // Attributes have to be array if ( ! ( isset ( $ attributes ) ) || ! is_array ( $ attributes ) ) { throw new \ InvalidArgumentException ( 'Undefined attributes' , 400 ) ; } // Perform before hook $ attributes = $ this -> beforeCreate ( $ attributes ) ; // Remove non fillable attributes $ fields = array_intersect_key ( $ attributes , array_flip ( $ this -> fillable ) ) ; // Validate attributes $ this -> setValidationRules ( ) ; $ errors = $ this -> validationErrors ( $ fields ) ; if ( $ errors ) { throw new Exceptions \ ValidationException ( 'Invalid attributes' , 400 , $ errors ) ; } // Create the model and return its ID $ result = $ this -> db ( ) -> table ( $ this -> table ) -> insertGetId ( $ fields ) ; // Return false if create fails if ( ! $ result ) { throw new \ Exception ( 'Could not create the model' , 500 ) ; } // Sync many to many relationships $ this -> sync ( $ result , $ attributes ) ; // Upsert has many relationships $ this -> updateHasMany ( $ result , $ attributes ) ; // Get the created model $ model = $ this -> find ( $ result ) ; // Run after hook $ model = $ this -> afterCreate ( $ result , $ attributes , $ model ) ; // Return the created model return $ model ; }
Creates a new model

@param $attributes Model attributes
@return bool|array Created model ID, or false
Creates a new model