Home > Software design >  Delete multiple record in laravel-8 that pass through URL
Delete multiple record in laravel-8 that pass through URL

Time:01-04

Through Passing single integer value , specified data is deleted from database.I want to pass multiple value and to delete corresponding data in database. what i want to?

Scenario is like that:

http://127.0.0.1:8000/dlt/15,16

i want to delete 15,15 record from database.Every time whenever i want to delete multiple record, i take above url and put value 1,3 or 3,4,5 or 6,7,8,9 after dlt

My web.php file

Route::get("/dlt/{id}",[MyController::class,"dtl"]);

MyController.php file where i specified function for delete operation

use App\Models\City;   
function dlt($id){

}

How can i do that.

How to delete multiple records using Laravel Eloquent

I have tried to understand from this above stackoverflow question but i did not understand because complete deletion process was not there.

Thanks in advance.

CodePudding user response:

It looks like you just want to delete an array of ID's, there is a method built in laravel to do this and it goes as follows:

City::destroy([1, 2, 3]);

Read more on it from: https://laravel.com/docs/9.x/eloquent#deleting-an-existing-model-by-its-primary-key

If you want to do it for a string you can follow this example:

$example = "1,2,2,2,3,3,3,4";
    
$results = array_unique(explode(',', $example));

City::destroy($results); //Here city is model name

Hopefully this will help you reach your goal

  • Related