如何删除Laravel中的重复行

I need to delete duplicated rows for specified Mobile Number on a mysql table. How can I do this with an Laravel query?

With Eloquent:

App\Model::where('mobile_number', '0123456789')->delete();

With the Query Builder:

DB::table('some_table')->where('mobile_number', '0123456789')->delete();

EDIT

The above will delete all rows with mobile_number 0123456789. If you want to keep one, use this:

// Get the row you don't want to delete.
$dontDeleteThisRow = App\Model::where('mobile_number', '0123456789')->first();

// Delete all rows except the one we fetched above.
App\Model::where('mobile_number', '0123456789')->where('id', '!=', $dontDeleteThisRow->id)->delete();

You could also do something like this, if you want to find duplicate values in the column 'name':

Example:

$duplicateRecords = DB::select('name')
              ->selectRaw('count(`name`) as `occurences`')
              ->from('users')
              ->groupBy('name')
              ->having('occurences', '>', 1)
              ->get();

Then you need to loop through your collection and delete the items.

foreach($duplicateRecords as $record) {
    $record->delete();
}

If you would like to leave every single entry and delete other duplicates.

The easiest way I found.

$same_data = DB::table('table_name')->where('mobile_number', '0000000000');

if ($same_data->count() > 1) {
    $same_data_before = clone $same_data;
    $top = $same_data->first();
    $same_data_before->where('id', '!=', $top->id)->delete();
}