public function update($id = null, $attributes = [])
    {
        // ID has to be defined
        if (!isset($id)) {
            throw new \InvalidArgumentException('Undefined ID to update', 400);
        }

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

        // Perform before hook
        $attributes = $this->beforeUpdate($id, $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);
        }

        // Update the model
        if ($fields && count($fields)) {
            $result = $this->db()->table($this->table)->where($this->primaryKey, $id, '=', $this->table)->update($fields);

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

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

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

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

        // Run after hook
        $model = $this->afterUpdate($id, $attributes, $model);

        // Return the created model
        return $model;
    }
public function update($id = null, $attributes = [])
    {
        // ID has to be defined
        if (!isset($id)) {
            throw new \InvalidArgumentException('Undefined ID to update', 400);
        }

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

        // Perform before hook
        $attributes = $this->beforeUpdate($id, $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);
        }

        // Update the model
        if ($fields && count($fields)) {
            $result = $this->db()->table($this->table)->where($this->primaryKey, $id, '=', $this->table)->update($fields);

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

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

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

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

        // Run after hook
        $model = $this->afterUpdate($id, $attributes, $model);

        // Return the created model
        return $model;
    }
public function update ( $ id = null , $ attributes = [ ] ) { // ID has to be defined if ( ! isset ( $ id ) ) { throw new \ InvalidArgumentException ( 'Undefined ID to update' , 400 ) ; } // Attributes have to be array if ( ! ( isset ( $ attributes ) ) || ! is_array ( $ attributes ) ) { throw new \ InvalidArgumentException ( 'Undefined attributes' , 400 ) ; } // Perform before hook $ attributes = $ this -> beforeUpdate ( $ id , $ 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 ) ; } // Update the model if ( $ fields && count ( $ fields ) ) { $ result = $ this -> db ( ) -> table ( $ this -> table ) -> where ( $ this -> primaryKey , $ id , '=' , $ this -> table ) -> update ( $ fields ) ; // Return false if the update failed if ( ! $ result ) { throw new \ Exception ( 'Could not update the model' , 500 ) ; } } // Sync many to many relationships $ this -> sync ( $ id , $ attributes ) ; // Upsert has many relationships $ this -> updateHasMany ( $ id , $ attributes ) ; // Get the created model $ model = $ this -> find ( $ id ) ; // Run after hook $ model = $ this -> afterUpdate ( $ id , $ attributes , $ model ) ; // Return the created model return $ model ; }
Updates a model

@param $id Model primary key
@param $attributes Model attributes
@return bool|array Updated model, or false
Updates a model