简体   繁体   中英

how to delete multiple records from database ( using laravel 5.5)

I am trying to delete multiple records from database but I am currently unable to do that. I can only delete first record from the database and I am using postman for my request.

My Code:

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

    $all_data = $request->all();

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

I am passing my value from postman like this: form-data:

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

If I hit my request it only deletes the record with id 3, but I want to delete multiple records. How can I do that?

Your help will be highly appreciated!

Try this

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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