Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I perform a Mass delete using Laravel 4.1, based on array of ids or objects?

I just wanted to know if it's possible.

I know when you have multiple rows to insert, you can just build an array and do something like:

DB::table('some_table')->insert($array);

But as far as I've read, doing the same for deleting doesn't seem to be possible, I'd like to know if anyone know of a way to do something like:

DB::table('some_table')->delete($array);
like image 980
arrigonfr Avatar asked May 13 '14 06:05

arrigonfr


People also ask

How can I delete data from multiple tables in single query in laravel?

Here is the code: $deleteQuery = "DELETE FROM posts WHERE user_id= " . $user->user_id . ";"; $deleteQuery .

How do I delete data in laravel?

Laravel Eloquent Deleting You can delete data after writing it to the database. You can either delete a model instance if you have retrieved one, or specify conditions for which records to delete. To delete a model instance, retrieve it and call the delete() method: $user = User::find(1); $user->delete();

How do I delete a specific row in laravel?

Step 1: Create Controller UserController by executing this command. Step 2: We can delete records in two ways. Second Method: The second way is to delete using the Laravel delete Function and User Model (Easy one). ->name( 'users.


1 Answers

Many ways of deleting records in Laravel 4.1

1) When you want to delete records from your database, simply call the delete method:

$affected = DB::table('users')->where('id', '=', 1)->delete();

2) Want to quickly delete a record by its ID? No problem. Just pass the ID into the delete method:

$affected = DB::table('users')->delete(1);

3) If you want to delete multiple records by id at once, passing their ids in an array - use the following

$users_to_delete = array(1, 2, 3);
DB::table('users')->whereIn('id', $users_to_delete)->delete(); 

4) If you want to delete multiple records by id at once, passing an array of users - use the following

        //(case A) User fields indexed by number 0,1,2..
        $users_to_delete = array(
           '0'=> array('1','Frank','Smith','Whatever'), 
           '1'=> array('5','John','Johnson','Whateverelse'),
        );

        $ids_to_delete = array_map(function($item){ return $item[0]; }, $users_to_delete);

        DB::table('users')->whereIn('id', $ids_to_delete)->delete(); 

        //(case B) User fields indexed by key
        $users_to_delete = array(
           '0'=> array('id'=>'1','name'=>'Frank','surname'=>'Smith','title'=>'Whatever'), 
           '1'=> array('id'=>'5','name'=>'John','surname'=>'Johnson','title'=>'Whateverelse'),
        );

        $ids_to_delete = array_map(function($item){ return $item['id']; }, $users_to_delete);

        DB::table('users')->whereIn('id', $ids_to_delete)->delete(); 

5) Deleting An Existing Model By Key

User::destroy(1);
User::destroy(array(1, 2, 3));
User::destroy(1, 2, 3);

6) Of course, you may also run a delete query on a set of models:

$affectedRows = User::where('votes', '>', 100)->delete();
like image 127
Gadoma Avatar answered Oct 15 '22 08:10

Gadoma