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` = ?