简体   繁体   English

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

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

I'd like to delete the rows in my table which are not in the array. 我想删除表中不在数组中的行。 In the following example, this will mass delete rows that correspond to $cards_to_delete. 在以下示例中,这将批量删除与$ cards_to_delete相对应的行。

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

How can I make it so it deletes everything that is NOT in the array? 我怎样才能使它删除数组中没有的所有内容? Something along these lines: 遵循以下原则:

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

Laravel provides a ->whereNotIn() method as well: Laravel还提供了->whereNotIn()方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM