Illuminate\Database\Eloquent\Model::performInsert PHP Method

performInsert() protected method

Perform a model insert operation.
protected performInsert ( Builder $query ) : boolean
$query Builder
return boolean
    protected function performInsert(Builder $query)
    {
        if ($this->fireModelEvent('creating') === false) {
            return false;
        }
        // First we'll need to create a fresh query instance and touch the creation and
        // update timestamps on this model, which are maintained by us for developer
        // convenience. After, we will just continue saving these model instances.
        if ($this->timestamps) {
            $this->updateTimestamps();
        }
        // If the model has an incrementing key, we can use the "insertGetId" method on
        // the query builder, which will give us back the final inserted ID for this
        // table from the database. Not all tables have to be incrementing though.
        $attributes = $this->attributes;
        if ($this->getIncrementing()) {
            $this->insertAndSetId($query, $attributes);
        } else {
            $query->insert($attributes);
        }
        // We will go ahead and set the exists property to true, so that it is set when
        // the created event is fired, just in case the developer tries to update it
        // during the event. This will allow them to do so and run an update here.
        $this->exists = true;
        $this->wasRecentlyCreated = true;
        $this->fireModelEvent('created', false);
        return true;
    }

Usage Example

示例#1
0
 /**
  * (non-PHPdoc)
  * @see \Illuminate\Database\Eloquent\Model::performInsert()
  */
 public function performInsert(Builder $query, array $options = [])
 {
     if (!Auth::user() instanceof User) {
         App::error(function (InvalidUserException $exception) {
             Log::error($exception);
             return 'Access denid for creating a new ' . get_called_class();
         });
     }
     $this->attributes['active'] = 1;
     $this->attributes[self::CREATED_AT] = new \DateTime('now', new \DateTimeZone(env('APP_TIMEZONE', 'UTC')));
     $this->attributes[self::CREATED_BY] = Auth::user()->id;
     $this->attributes[self::UPDATED_AT] = new \DateTime('now', new \DateTimeZone(env('APP_TIMEZONE', 'UTC')));
     $this->attributes[self::UPDATED_BY] = Auth::user()->id;
     return parent::performInsert($query, $options);
 }
All Usage Examples Of Illuminate\Database\Eloquent\Model::performInsert
Model