Eloquent如何刪除一筆資料呢?
$user = User::find(1); $user->delete();
/**
* Delete the model from the database.
*
* @return bool|null
* @throws \Exception
*/
public function delete()
{
if (is_null($this->primaryKey))
{
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;
}
}
/**
* Perform the actual delete query on this model instance.
*
* @return void
*/
protected function performDeleteOnModel()
{
$this->newQuery()->where($this->getKeyName(), $this->getKey())->delete();
}