Deleting Models
To delete a model, call the delete
method on a model instance:
$user = App\User::find(1);
$user->delete();
Deleting An Existing Model By Key
In the example above, we are retrieving the model from the database before calling the delete
method. However, if you know the primary key of the model, you may delete the model without explicitly retrieving it by calling the destroy
method. In addition to a single primary key as its argument, the destroy
method will accept multiple primary keys, an array of primary keys, or a collection of primary keys:
App\User::destroy(1);
App\User::destroy(1, 2, 3);
App\User::destroy([1, 2, 3]);
App\User::destroy(collect([1, 2, 3]));
The destroy
method loads each model individually and calls the delete
method on them so that the deleting
and deleted
events are fired.
Deleting Models By Query
You can also run a delete statement on a set of models. In this example, we will delete all users that are marked as inactive. Like mass updates, mass deletes will not fire any model events for the models that are deleted:
$deletedRows = App\User::where('active', 0)->delete();
When executing a mass delete statement via Eloquent, the deleting
and deleted
model events will not be fired for the deleted models. This is because the models are never actually retrieved when executing the delete statement.
Soft Deleting
In addition to actually removing records from your database, Eloquent can also "soft delete" models. When models are soft deleted, they are not actually removed from your database. Instead, a deleted_at
attribute is set on the model and inserted into the database. If a model has a non-null deleted_at
value, the model has been soft deleted. To enable soft deletes for a model, use the Illuminate\Database\Eloquent\SoftDeletes
trait on the model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
use SoftDeletes;
}
The SoftDeletes
trait will automatically cast the deleted_at
attribute to a DateTime
/ Carbon
instance for you.
You should also add the deleted_at
column to your database table. The Laravel schema builder contains a helper method to create this column:
public function up()
{
Schema::table('Users', function (Blueprint $table) {
$table->softDeletes();
});
}
public function down()
{
Schema::table('Users', function (Blueprint $table) {
$table->dropSoftDeletes();
});
}
Now, when you call the delete
method on the model, the deleted_at
column will be set to the current date and time. And, when querying a model that uses soft deletes, the soft deleted models will automatically be excluded from all query results.
To determine if a given model instance has been soft deleted, use the trashed
method:
if ($user->trashed()) {
//
}
Querying Soft Deleted Models
Including Soft Deleted Models
As noted above, soft deleted models will automatically be excluded from query results. However, you may force soft deleted models to appear in a result set using the withTrashed
method on the query:
$user = App\User::withTrashed()
->where('account_id', 1)
->get();
The withTrashed
method may also be used on a relationship query:
$user->history()->withTrashed()->get();
Retrieving Only Soft Deleted Models
The onlyTrashed
method will retrieve only soft deleted models:
$user = App\Flight::onlyTrashed()
->where('airline_id', 1)
->get();
Restoring Soft Deleted Models
Sometimes you may wish to "un-delete" a soft deleted model. To restore a soft deleted model into an active state, use the restore
method on a model instance:
$user->restore();
You may also use the restore
method in a query to quickly restore multiple models. Again, like other "mass" operations, this will not fire any model events for the models that are restored:
App\User::withTrashed()
->where('id', 1)
->restore();
Like the withTrashed
method, the restore
method may also be used on relationships:
$user->history()->restore();
Permanently Deleting Models
Sometimes you may need to truly remove a model from your database. To permanently remove a soft deleted model from the database, use the forceDelete
method:
// Force deleting a single model instance...
$user->forceDelete();
// Force deleting all related models...
$user->history()->forceDelete();