我想删除我的表中不属于数组的行。在下面的示例中,这将大量删除与$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();