我想删除表中不在数组中的行。在以下示例中,这将批量删除与 $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();
关于php - 如何使用 Laravel 4.2 批量删除数据库表中不在数组中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31627305/