如何从数据库中删除多条记录(使用laravel 5.5)

时间:2018-05-04 12:33:00

标签: php arrays laravel

我正在尝试从数据库中删除多条记录,但我目前无法做到这一点。我只能从数据库中删除第一条记录,而我正在使用邮递员来处理我的请求。

我的代码:

Route::post('delete_page',function(Request $request) {

    $all_data = $request->all();

    foreach($all_data as $id) {
        \App\Page::where('id',$id)->delete();
    }
});

我从邮递员那里传递我的价值: 形状数据:

key is: array[id] and value is any id that is 3
key is: array[id] and value is any id that is 4

如果我点击我的请求它只会删除ID为3的记录,但我想删除多条记录。我怎么能这样做?

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

试试这个

$ ids = [1,2,3,4];

\ App \ Page :: whereIn(' id',$ ids) - > delete();

相关问题