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

delete() public method

Delete the model from the database.
public delete ( ) : boolean | null
return boolean | null
    public function delete()
    {
        if (is_null($this->getKeyName())) {
            throw new Exception('No primary key defined on model.');
        }
        if ($this->exists) {
            if ($this->fireModelEvent('deleting') === false) {
                return false;
            }
            // Here, we'll touch the owning models, verifying these timestamps get updated
            // for the models. This will allow any caching to get broken on the parents
            // by the timestamp. Then we will go ahead and delete the model instance.
            $this->touchOwners();
            $this->performDeleteOnModel();
            $this->exists = false;
            // Once the model has been deleted, we will fire off the deleted event so that
            // the developers may hook into post-delete operations. We will then return
            // a boolean true as the delete is presumably successful on the database.
            $this->fireModelEvent('deleted', false);
            return true;
        }
    }

Usage Example

Example #1
1
 /**
  * Removes a model from this relationship type.
  */
 public function remove(Model $model, $sessionKey = null)
 {
     if ($sessionKey === null) {
         $options = $this->parent->getRelationDefinition($this->relationName);
         if (array_get($options, 'delete', false)) {
             $model->delete();
         } else {
             /*
              * Make this model an orphan ;~(
              */
             $model->setAttribute($this->getPlainForeignKey(), null);
             $model->setAttribute($this->getPlainMorphType(), null);
             $model->save();
         }
         /*
          * Use the opportunity to set the relation in memory
          */
         if ($this instanceof MorphOne) {
             $this->parent->setRelation($this->relationName, null);
         } else {
             $this->parent->reloadRelations($this->relationName);
         }
     } else {
         $this->parent->unbindDeferred($this->relationName, $model, $sessionKey);
     }
 }
All Usage Examples Of Illuminate\Database\Eloquent\Model::delete
Model