Skip to content

Instantly share code, notes, and snippets.

@nguyenthanhtung88
Forked from systemseven/model.php
Created May 11, 2016 06:42
Show Gist options
  • Save nguyenthanhtung88/94610d85358b9698fb139c436e62ebeb to your computer and use it in GitHub Desktop.
Save nguyenthanhtung88/94610d85358b9698fb139c436e62ebeb to your computer and use it in GitHub Desktop.
Soft Deletes in Laravel 4
//in the model
class User extends Eloquent {
protected $softDelete = true;
}
//in the migration, to create a soft delete column
$table->softDeletes();
//to force query to return the deleted items
$users = User::withTrashed()->where('account_id', 1)->get();
//to have query return ONLY the deleted items
$users = User::onlyTrashed()->where('account_id', 1)->get();
//to undelete a soft deleted model
$user->restore();
//or use the restore method on a query
User::withTrashed()->where('account_id', 1)->restore();
//to full delete, not soft delete
$user->forceDelete();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment