-
-
Save nguyenthanhtung88/94610d85358b9698fb139c436e62ebeb to your computer and use it in GitHub Desktop.
Soft Deletes in Laravel 4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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