如何使用Laravel 4.2批量删除数据库表中不在数组中的行


How to mass delete rows in database table that are NOT in array using Laravel 4.2

我想删除我的表中不属于数组的行。在下面的示例中,这将大量删除与$cards_to_delete对应的行。

$cards_to_delete = array(1, 2, 3);
Collection::where('username', '=', $username)
    ->whereIn('id', $cards_to_delete)
    ->delete();

我怎么能使它删除一切不是在数组?下面的内容:

$cards_to_keep = array(1, 2, 3);
Collection::where('username', '=', $username)
    ->whereIn('id', '!=', $cards_to_keep)
    ->delete();

Laravel也提供了一个->whereNotIn()方法:

$cards_to_keep = array(1, 2, 3);
Collection::where('username', '=', $username)
    ->whereNotIn('id', $cards_to_keep)
    ->delete();