How to delete multiple records using Laravel Eloquent
You can use the destroy()
method in Laravel Eloquent to delete multiple records. The destroy()
method accepts an array of primary keys and deletes the corresponding records in the database.
Example:
<?php
$ids = [1, 2, 3];
Model::destroy($ids);
This will delete the records with primary key values of 1, 2, and 3 from the table associated with the Model
class.
Alternatively, you can use a query builder to delete multiple records by chaining the whereIn()
and delete()
method:
<?php
$ids = [1, 2, 3];
DB::table('table_name')->whereIn('id', $ids)->delete();
This will delete all rows from the table_name where id is in the array $ids.
Please keep in mind that both of these methods delete the records permanently from the database.