Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between destroy() and delete() methods in Laravel?

People also ask

What is difference between destroy and delete in Rails?

Basically destroy runs any callbacks on the model while delete doesn't. Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can't be persisted).

What is Delete method in laravel?

Laravel Eloquent DeletingTo delete a model instance, retrieve it and call the delete() method: $user = User::find(1); $user->delete(); Alternatively, you can specify a primary key (or an array of primary keys) of the records you wish to delete via the destroy() method: User::destroy(1); User::destroy([1, 2, 3]);

How do I delete a record in laravel?

Step 1: Create Controller UserController by executing this command. Step 2: We can delete records in two ways. Second Method: The second way is to delete using the Laravel delete Function and User Model (Easy one). ->name( 'users.


  • destroy is correct method for removing an entity directly (via object or model).

Example:

$teetime = Teetime::where('date', '=', $formattedDate)->firstOrFail();
$teetime->destroy();
  • delete can only be called in query builder

Example:

$teetime = Teetime::where('date', '=', $formattedDate)->delete();

From documentation:

Deleting An Existing Model By Key

User::destroy(1);

User::destroy(array(1, 2, 3));

User::destroy(1, 2, 3);

Of course, you may also run a delete query on a set of models:

$affectedRows = User::where('votes', '>', 100)->delete();

More info: http://laravel.com/docs/eloquent


The accepted answer may have been true in 2014, I'm not sure. It is certainly not correct now.

The delete() method is called against an instance of a model class. Using the original question's code:

$teetime = Teetime::where('date', '=', $formattedDate)->firstOrFail();
// $teetime is a model instance
$teetime->delete();

The destroy() method is a static method of the model class. It accepts one or multiple primary keys:

$teetime_ids = Teetime::where('date', '=', $formattedDate)->pluck('id');
// $teetime_ids is a collection of 1 or more primary keys
Teetime::destroy($teetime_ids);

From the documentation:

Deleting Models

To delete a model, you may call the delete method on the model instance:

use App\Models\Flight;
$flight = Flight::find(1);
$flight->delete();

Deleting An Existing Model By Its Primary 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 accepting the single primary key, the destroy method will accept multiple primary keys, an array of primary keys, or a collection of primary keys:

Flight::destroy(1);
Flight::destroy(1, 2, 3);
Flight::destroy([1, 2, 3]);
Flight::destroy(collect([1, 2, 3]));

There is also a delete() query builder method that allows you to directly delete records from the database table. There are important caveats for using this method, and it's important to understand that Eloquent implements its own query builder class, much as it does its own collection class.

When called against an Eloquent query builder, certain behaviour such as support for soft deletes is preserved. However, Eloquent events deleting and deleted are not fired, as a model is never created.

delete() can also be called against the base query builder class, which has no knowledge of Eloquent at all.

Compare the generated queries, assuming that Teetime imports the SoftDeletes trait:

Teetime::where('date', '=', $formattedDate)->delete();
// UPDATE teetimes SET deleted_at = NOW(), updated_at = NOW() WHERE `date` = ?
DB::table('teetimes')->where('date', '=', $formattedDate)->delete();
// DELETE FROM teetimes WHERE `date` = ?