如何使用 Laravel 4.2 批量删除数据库 table 中不在数组中的行
How to mass delete rows in database table that are NOT in array using Laravel 4.2
我想删除 table 中不在数组中的行。在下面的示例中,这将批量删除对应于 $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();
我想删除 table 中不在数组中的行。在下面的示例中,这将批量删除对应于 $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();