19

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);
Lee Davis
  • 4,685
  • 3
  • 28
  • 39
arrigonfr
  • 742
  • 3
  • 12
  • 34
  • this may help? http://stackoverflow.com/questions/21747529/laravel-how-to-delete-rows-from-multiple-table-with-same-id-with-only-1-query – George G May 13 '14 at 07:03
  • actually,i've already done that. It's easy because in that case you're just deleting every record where one attribute equals a specific value. In my case, on some cases I need to build an array with three different attributes, and each attribute may take different values... – arrigonfr May 13 '14 at 07:07
  • Say.. $array=array(array('1'=>1, '2'=>2, '3'=>3), array('1'=>4, '2'=>5, '3'=>6), array('1'=>7, '2'=>8, '3'=>9), array('1'=>0, '2'=>1, '3'=>2)); What I want to avoid is putting the delete query inside a foreach loop. – arrigonfr May 13 '14 at 07:11
  • Maybe using the type of syntax in this [SQLFiddle](http://sqlfiddle.com/#!2/28eba), if i build an arrays with just the values and no keys i could do something like: DB::table('some_table')->whereIn('attr1, attr2, attr3', $array)->delete(); – arrigonfr May 13 '14 at 07:27
  • Unfortunately I'm not familiar with Laravel yet, so I can't say anything more :/ – George G May 13 '14 at 07:32

1 Answers1

46

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();
Gadoma
  • 6,475
  • 1
  • 31
  • 34